二、url重定向
rewrite:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root F:\\dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /back {
rewrite_log on;
#rewrite /back /user/kaptcha break;
#rewrite正则重定向
rewrite ^/back(.*)$ /$1 break;
proxy_pass http://localhost:8888;
}
}
http://localhost/back/user/kaptcha将在nginx url中重定向到http://localhost:8888/user/kaptcha。
reg为^/back(.*)$, replacement为/$1 其中变量$1代表reg中的(.*), flag为break。
也就是nginx中url /back部分将被/替换。而剩余的url部分原样不动。
flag::break 本条规则匹配完成后,终止匹配,不再匹配后面的规则,浏览器地址栏URL地址不变;一般使用在location中;
flag::last 本条规则匹配完成后,继续向下匹配新的location URI规则,浏览器地址栏URL地址不变;
flag::redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址;
flag::permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
注意本地开发时nginx域名与后端应用的域名一致,如nginx使用127.0.0.1而后端应用使用localhost,则跨域domain不一致,导致jsessionid不能写入cookie,后端sessionid随请求时刻变化。