nginx代理 指向_nginx代理及重定向之一

30号接到客户要求,需要将其官方域名默认页面更改,并嵌套于项目中。

本身项目域名是www.qin.com和www.qin.cn,增加官网域名le.qin.com和le.qin.cn;项目域名是访问项目默认首页,现在需要官网域名访问指定首页。首先就想到重定向域名到其页面,实施步骤如下:

1,在nginx配置文件中server下

server {

listen       80;

server_name  le.qin.com le.qin.cn;

if ($host = 'leda.qinba56.cn' ) {

rewrite ^(.*) http://www.qin.cn/officialsite/initLedaIndex permanent;

}

if ($host = 'leda.qinba56.com' ) {

rewrite ^(.*) http://www.qin.com/officialsite/initLedaIndex permanent;

}

location / {

proxy_pass http://127.0.0.1:9002/;

root   html;

index  index.html index.htm index.jsp index.jspx;

if ($request_uri ~* "\.(js|css|png|jpeg|jpg|bmp|mp3|swf|avi|flv)$"){

expires 1d;

}

}

}

这样倒是能实现,不过访问时地址会由:

le.qin.cn跳转到http://www.qin.cn/officialsite/initLedaIndex

le.qin.com跳转到http://www.qin.com/officialsite/initLedaIndex

但客户要求的是页面内容变地址不变,故此方法取消;

2,利用nginx反向代理proxy_pass

server {

listen       80;

server_name  le.qin.com le.qin.cn;

location / {

proxy_pass http://127.0.0.1:9002/officialsite/initLedaIndex;

root   html;

index  index.html index.htm index.jsp index.jspx;

if ($request_uri ~* "\.(js|css|png|jpeg|jpg|bmp|mp3|swf|avi|flv)$"){

expires 1d;

}

}

}

这样能访问页面,但是只显示文字部分,其他显示不出来;

3,还是重定向

location / {

proxy_pass http://127.0.0.1:9002/;

root   html;

index  index.html;

rewrite "^/+$" /officialsite/initLedaIndex permanent;

}

报错404

4,后想到工程使用了session,故网上搜索了一下

Nginx作为反向代理到Tomcat应用时,session丢失,路径找不到

故在location / {}里加入以下代码:

location / {

proxy_pass http://127.0.0.1:9002/;

root   html;

index  index.html;

rewrite "^/+$" /officialsite/initLedaIndex permanent;

proxy_cookie_path /officialsite/initLedaIndex/ /;

proxy_cookie_path /officialsite/initLedaIndex /;

proxy_set_header   Cookie $http_cookie;

proxy_set_header        Host    $http_host;

}

就可以访问了,不过访问地址为

http://le.qin.com/officialsite/initLedaIndex

http://le.qin.com/officialsite/initLedaIndex

5,还是没有实现地址不变的目的

location / {

proxy_pass http://127.0.0.1:9002/;

root   html;

index  index.html;

rewrite "^/+$" /officialsite/initLedaIndex permanent;

}

将permanent 更改为last

rewrite "^/+$" /officialsite/initLedaIndex last;

最后变成:

server {

listen       80;

server_name  le.qin.com le.qin.cn;

location / {

proxy_pass http://127.0.0.1:9002/;

root   html;

index  index.html;

rewrite "^/+$" /officialsite/initLedaIndex last;

#proxy_cookie_path /offIcialsite/initLedaIndex/ /;

proxy_cookie_path /offIcialsite/initLedaIndex /;

proxy_set_header   Cookie $http_cookie;

proxy_set_header   Host    $http_host;

proxy_set_header   X-Real-IP       $remote_addr;

proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

}终于还是实现了用le.qin.com或le.qin.cn访问新页面而且地址栏地址不变

本文转自 wdy198622 51CTO博客,原文链接:http://blog.51cto.com/weimouren/1845096

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值