nginx 匹配(重定向)

2025-04-08 07:34:23

1、精准匹配location / { root /www; index aa.html index.html index.htm; } location = /{ root html; index index.php; } 先执行index.php http://192.168.0.89/aa.html 这个会访问 www下的aa.html

2、正则匹配查找匹配/www/imagelocation ~ image { root /www; index index.html; } 在/www/image下查找图片精准匹配->普通匹配->正则

3、安装echo模块1. 首先下载模块源码:https://github.com/agentzh/echo-nginx-module/tags2. 解压到某个路径,假设为 /path/to/echo-nginx-module3. 使用下面命令编译并安装 Nginxwget 'http://sysoev.ru/nginx/nginx-1.0.11.tar.gz' $ tar -xzvf nginx-1.0.11.tar.gzcd nginx-1.0.11/ # Here we assume you would install you nginx under /opt/nginx/../configure --prefix=/opt/nginx \ --add-module=/path/to/echo-nginx-modulemake -j2make install切记先停止nginx然后 重新加载配置文件启动nginx/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

4、nginx 语法: “=”判断相等 “~”正则匹配(此处区分大小写) ~* 不区分大小写匹配 !~:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时区分字符大小写; !~*:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时不区分字符大小写;文件/文件夹判断if ( $http_user_agent ~ Trident ){ rewrite ^.* index.html; break;# return 404; } -f, !-f:判断指定的路径是否为存在且为文件;-d, !-d:判断指定的路径是否为存在且为目录;-e, !-e:判断指定的路径是否存在,文件或目录均可; -x, !-x:判断指定路径的文件是否存在且可执行; location /hello { default_type 'text/html'; set $u $document_root$fastcgi_script_name; if ( !-d $u ) { return 404; } }引用别人的http://blog.csdn.net/iinel/article/details/4321383利用more fastcgi.conf可以查看nginx的变量

5、调到指定域名if ( $host = '192.168.0.89争犸禀淫' ){ # default_type 'text/html'; #echo $QUERY_STRING; rewrite ^/(.*)$ http:shangqianqian.com/$1 permanent ; } 解释一下这几个东西:last: 相当于Apache的[L]标记,表示完成rewritebreak: 停止执行当前虚拟主机的后续rewrite指令集redirect: 返回302临时重定向,地址栏会显示跳转后的地址permanent: 返回301永久重定向,地址栏会显示跳转后的地址因为301和302不能简单的只返回状态码,还必须有重定向的URL,这就是return指令无法返回301,302的原因了。这里 last 和 break 区别有点难以理解: last一般写在server和if中,而break一般使用在location中 last不终止重写后的url匹配,即新的url会再从server走一遍匹配流程,而break终止重写后的匹配 break和last都能组织继续执行后面的rewrite指令 rewrite index.html index.php last; index.html跳转到index.php文件 rewrite ^/(\d+)\.html index_$1.php last; 输入2.html实际访问的是index_2.php rewrite ^/image/(\d+) /img/$1.jpg last; 输入http://192.168.0.89/image/2实际访问的是根下的img/2.jpg rewrite ^/(\d+) /$1.jpg last; 输入这个http://192.168.0.89/2实际访问的是根路径下的2.jpg

6、1.html 文件添加如下代码<img src='./photo/1.jpg'/>nginx.conf添加如下代码location ~ photo { root /www/img; }这个图片实际访问的就是/www/img/photo/1.jpglocation ~ photo { # root /www/img; 重定向到别的文件下 # rewrite ^/[a-w0-9A_W/\.]+/(.*?)$ /www/img/photo/$1 break; 测试好像只能在根目录下}可以瞅瞅这个:https://segmentfault.com/a/1190000002797606#articleHeader6

nginx 匹配(重定向)

7、文件供参考(有点乱) log_format main '$remote_addr - $r髫潋啜缅emote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '"$upstream_addr" "$upstream_response_time" "$request_time"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; if ( $host = '192.168.0.88' ){ # default_type 'text/html'; #echo $QUERY_STRING; rewrite ^/(.*)$ http://opq.renqibaohe.com/$1 permanent ; } #limit_conn test 1; #charset koi8-r; #access_log logs/host.access.log main; rewrite /image/tmig.jpg /img/tmig.jpg break; location /hello {# default_type 'text/html';# echo $QUERY_STRING;# set $u $document_root$fastcgi_script_name;# echo $u; root /www; index index.html; rewrite ^/image/(.*)$ /img/$1 last;# if ( !-d $u ) {# return 404;# } } location =/index.html { root /www; default_type 'text/html'; echo $document_root$fastcgi_script_name; index aa.html index.html index.htm; }#location = / {# proxy_pass http://www.baidu.com;#} location /{ root html ; default_type 'text/html'; #如果没有这个default_type,则会一直下载文件而不是输出在浏览器上 #echo $echo_client_request_headers; #echo_read_request_body; #echo $request_body; echo $http_user_agent; if ( $http_user_agent ~ Trident ){ rewrite ^.* index.html; break;# return 404; } if ( $remote_addr = '192.168.0.107'){ echo "delay"; } if ( -e $document_root$fastcgi_script_name ) { #echo $document_root$fastcgi_script_name; #rewrite ^.* index.html; return 404;# break; } echo $request_method; echo 'fjslfdjdslfjs'; index index.php; } location /index.html{ root html; index index.html; } #location ~ image {# rewrite ^/image/(.*) /img/$1 last;# root /www; #} location /foo { default_type 'text/html'; #如果没有这个default_type,则会一直下载文件而不是输出在浏览器上 echo 'fsfsd'; #if($host != '192.168.0.107'){ #default_type 'text/javascript'; #如果没有这个default_type,则会一直下载文件而不是输出在浏览器上 # echo $echo_client_request_headers; # echo_read_request_body; # echo $request_body; #} } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; #location = /50x.html { # root html; #}# location =/1.html{# # root /usr/local/nginx/html; # limit_req zone=allips burst=5 nodelay; # } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/local/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # echo "delay"; } if ( -e $document_root$fastcgi_script_name ) { #echo $document_root$fastcgi_script_name; #rewrite ^.* index.html; return 404;# break; } echo $request_method; echo 'fjslfdjdslfjs'; index index.php; } location /index.html{ root html; index index.html; } #location ~ image {# rewrite ^/image/(.*) /img/$1 last;# root /www; #} location /foo { default_type 'text/html'; #如果没有这个default_type,则会一直下载文件而不是输出在浏览器上 echo 'fsfsd'; #if($host != '192.168.0.107'){ #default_type 'text/javascript'; #如果没有这个default_type,则会一直下载文件而不是输出在浏览器上 # echo $echo_client_request_headers; # echo_read_request_body; # echo $request_body; #} }

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢