配置文件意思就是伪装一下URL,如/index.jsp?pageNum=2伪装成/2.html,这样的话,你访问/2.html其实跳转的真实地址是/index.jsp?pageNum=2
server {listen 82;listen [::]:82;#root /var/www/web/index;#index index.html;server_name localhost;location / {rewrite ^/2.html$ /index.jsp?pageNum=2 break;proxy_pass http://192.168.xxx.xxx:8080;#try_files $uri $uri/ =404;}location ~*/(js|img|css) {root html;index index.html index.htm;}}
rewrite ^/2.html$ /index.jsp?pageNum=2 break;
^$是正则的开头和结尾标志,里面/2.heml可以换成其他的正则形式,比如
^([0-9]+).html$ /index.jsp?pageNum=$1 break;
&1表示第一个匹配上的规则,如果匹配的规则很多,可以加$2 $3等。
关于break是转发规则:
负载均衡
当然,伪静态不影响负载均衡,可以同时实现
server {listen 82;listen [::]:82;#root /var/www/web/index;#index index.html;upstream totalurl {server 192.168.xxx.xxx:8080 weight=8 break;server 192.168.yyy.yyy:8080 weight=8 break;}server_name localhost;location / {rewrite ^/2.html$ /index.jsp?pageNum=2 break;proxy_pass http://totalurl;#try_files $uri $uri/ =404;}location ~*/(js|img|css) {root html;index index.html index.htm;}}