Discuz!大师网

搜索
查看: 2964|回复: 1

Nginx rewrite伪静态配置及参数详解

[复制链接]
发表于 2013-6-6 22:02:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x

nginx rewrite 伪静态配置参数和使用例子 附正则使用说明

正则表达式匹配,其中:

  • * -f和!-f用来判断是否存在文件
  • * -d和!-d用来判断是否存在目录
  • * -e和!-e用来判断是否存在文件或目录
  • * -x和!-x用来判断文件是否可执行

flag标记有:

  • $args
  • $content_length
  • $content_type
  • $document_root
  • $document_uri
  • $host
  • $http_user_agent
  • $http_cookie
  • $limit_rate
  • $request_body_file
  • $request_method
  • $remote_addr
  • $remote_port
  • $remote_user
  • $request_filename
  • $request_uri
  • $query_string
  • $scheme
  • $server_protocol
  • $server_addr
  • $server_name
  • $server_port
  • $uri

结合QeePHP的例子

  • if ($host ~* (.*)\.domain\.com) {
  • set $sub_name $1;
  • rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
  • }

目录对换
/123456/xxxx -> /xxxx?id=123456

  • if ($http_user_agent ~ MSIE) {
  • rewrite ^(.*)$ /nginx-ie/$1 break;
  • }

目录自动加“/”

  • location ~/\.ht {
  • deny all;
  • }

禁止多个目录

  • location ~ ^/data {
  • deny all;
  • }

禁止单个目录
不能禁止.log.txt能请求

  • location ~ /data/sql/data.sql {
  • deny all;
  • }

给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99 天,robots.txt为7天并不记录404错误日志

  • location ^~ /html/scripts/loadhead_1.js {
  • access_log off;
  • root /opt/lampp/htdocs/web;
  • expires 600;
  • break;
  • }

文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存

  • root /opt/htdocs/www;
  • allow 208.97.167.194;
  • allow 222.33.1.2;
  • allow 231.152.49.4;
  • deny all;
  • auth_basic "C1G_ADMIN";
  • auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向/job/123/456/789.html

  • rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;

上面例子有个问题是访问/shanghai 时将不会匹配

  • if (-d $request_filename){
  • rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  • }

知道原因后就好办了,让我手动跳转吧

域名跳转

三级域名跳转

  • server
  • {
  • listen 80;
  • server_name mirror.c1gstudio.com;
  • index index.html index.htm index.php;
  • root /opt/lampp/htdocs/www;
  • rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
  • access_log off;
  • }

某个子目录作镜向

  • rewrite ^/(space|network)-(.+)\.html$ /$1.php?rewrite=$2 last;
  • rewrite ^/(space|network)\.html$ /$1.php last;
  • rewrite ^/([0-9]+)$ /space.php?uid=$1 last;

discuz 7 rewrite

discuz ucenter 头像 rewrite 优化

  • location ~ .*\.php?$
  • {
  • #fastcgi_pass unix:/tmp/php-cgi.sock;
  • fastcgi_pass 127.0.0.1:9000;
  • fastcgi_index index.php;
  • include fcgi.conf;
  • }
  • location ~* ^/index.php/
  • {
  • rewrite ^/index.php/(.*) /index.php?$1 break;
  • fastcgi_pass 127.0.0.1:9000;
  • fastcgi_index index.php;
  • include fcgi.conf;
  • }

回复 马甲回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|网站地图|小黑屋|展会网|Discuz站长论坛 |天天打卡

GMT+8, 2024-5-8 11:16 , Processed in 0.024091 second(s), 6 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表