nginx使用技巧(二)
1.转发
nginx域名隐性(地址栏域名不变)跳转
1、完全url的域名隐性跳转
server_name a.b.com
location / {
proxy_pass http://x.y.com;
}
效果:浏览器地址栏中输入a.b.com域名不变访问的内容为x.y.com上的内容
2、部分url的域名隐性跳转到非根目录下的内容
server_name a.b.com
location ~ .*\.(js|css)$ {
rewrite ^/(.*) /js/$1 break;
proxy_pass http://x.y.com;
}
效果:浏览器地址栏中输入a.b.com以.js或.css结尾的url,域名不变其实访问的内容是x.y.com/js/下的内容。
2.域名301转发
www.uu66.com.conf
#
server {
listen 80;
server_name www.uu66.com uu66.com;
index index.html index.htm index.php;
#root /data/www/sso.uu66.com/backend/web/;
access_log /data/log/nginx/access_uu66.com_80.log main buffer=10k flush=5s;
if ( $host != "lf.uu66.com" )
{
rewrite ^/(.*)$ http://lf.uu66.com/$1 permanent;
}
#location / {
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# #proxy_set_header Destination $fixed_destination;
# proxy_pass_header Set-Cookie;
# proxy_pass http://lf.uu66.com;
#}
}
3.跨域访问
Access-Control-Allow-Origin
add_header Access-Control-Allow-Origin *;
4.端口转发
http://18.18.32.32:28090 访问这个最终路径跳到:
http://18.18.32.32/login.action?os_destination=%2Findex.action&permissionViolation=true
server_name_in_redirect off;
proxy_set_header Host $host:28090;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.72.14.2:8090;
关键配置
指定端口转发
5.proxy_pass转发
Nginx配置proxy_pass转发的/路径问题
在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}
如上面的配置,如果请求的url是http://servername/static_js/test.html
会被代理成http://js.test.com/test.html
而如果这么配置
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com;
}
则会被代理到http://js.test.com/static_js/test.htm
更多推荐


所有评论(0)