nginx 常用命令

语法规则
location [=||*|^~] /uri/ {… }

符号	含义
=	精确匹配 
^~	非正则匹配
~	正则匹配(区分大小写)
~*	正则匹配(不区分大小写)
!~	正则不匹配(区分大小写)
!~*	正则不匹配(不区分大小写)
 	普通匹配(这里没有符号的时候)

匹配规则

  1. 精准匹配命中时,停止location

2.一般匹配(普通和非正则)命中时,对比所有命中的一般匹配,选出最长的一条

3.如果最长的那一条为非正则匹配,直接匹配此条,停止location

4.如果最长的那一条为普通匹配,继续尝试正则location(以上至此都不存在代码顺序)

5.按代码顺序执行正则匹配,当第一条正则location命中时,停止location

查出nginx所在目录

ps -ef|grep nginx
重新加载配置
/usr/sbin/nginx -s reload

启动
直接nginx

Nginx rewrite 和 proxy_pass共用

地址栏会发生变化

server {
		listen       19001;
		server_name  localhost;
		charset UTF-8;
		client_max_body_size 1000m;
		location /tmp/img {
			rewrite ^/tmp/img/(.*)$ http://100.*.*.*:9001/$1;
		}
	}

地址栏不会发生变化

server {
		listen       19001;
		server_name  localhost;
		charset UTF-8;
		client_max_body_size 1000m;
		location /tmp/img {
			# /$1的意思是, 先将 /tmp/img 删除, 只保留其后面的路径
			rewrite ^/tmp/img/(.*)$ /$1 break;
			# 改写完之后, 再进行代理; 最终结果: http://100.*.*.*:9001/$1 
			proxy_pass  http://100.*.*.*:9001;
		}
	}



  location /ag-crm-server/ {
             rewrite ^/ag-crm-server/(.*)$ /ag-crm-server/$1 break;
             proxy_pass http://sp.xxxxxx.com:8090;
             proxy_set_header   X-Forwarded-Proto $scheme;
             proxy_set_header   X-Real-IP         $remote_addr;
        }

反向代理时,get 中的 url 参数丢失

现象

required string parameter ‘XXX‘is not present


		 location  /java/far {

	    	  rewrite ^/java/far/(.*)$ /$1 break;
            proxy_pass http://127.0.0.1:7777/far/$1?$args;

		 	proxy_set_header X-Forwarded-Proto $scheme;
			proxy_set_header X-Real-IP $remote_addr;
            


        }

注意后面的 ?$args 是关键...
上面的只能应对 xxxx.com/xx?id=xx

更好的解决办法

   location ~ /xzapi/(.*) {
            proxy_pass      http://127.0.0.1:8080/$1$is_args$args; 
        }

这样可以兼容 xxxx.com/xx?id=xx 以及 post类的接口 如 xxxx.com/xx/login

posted @ 2022-03-12 13:09  方东信  阅读(471)  评论(0编辑  收藏  举报