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


相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
6月前
|
网络协议 应用服务中间件 nginx
nginx配置tcp协议代理的日志
nginx配置tcp协议代理的日志
148 0
|
7月前
|
Prometheus Cloud Native 应用服务中间件
nginx 代理 prometheus
nginx 代理 prometheus
120 0
|
7月前
|
存储 应用服务中间件 文件存储
Nginx代理作为文件服务器
Nginx代理作为文件服务器
|
7月前
|
应用服务中间件 nginx
|
5月前
|
缓存 JavaScript 应用服务中间件
Nginx+Tomcat代理环境下JS无法完全加载问题
Nginx+Tomcat代理环境下JS无法完全加载问题
|
1月前
|
数据可视化 应用服务中间件 网络安全
简单易用的Nginx代理管理工具:体验便捷配置、高效管理
Nginx Proxy Manager是一款强大的代理服务器管理工具,提供简单直观的界面来配置和管理Nginx代理服务器,帮助用户轻松提升配置的简洁性和便捷性。
55 0
简单易用的Nginx代理管理工具:体验便捷配置、高效管理
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
224 0
|
3月前
Nginx---代理遇到的坑
Nginx---代理遇到的坑
18 1
|
4月前
|
域名解析 网络协议 应用服务中间件
百度搜索:蓝易云【服务器配置到云上nginx代理?】
现在,您的云服务器已经配置为使用Nginx代理了。通过访问您的域名,请求将被转发到云服务器上的指定端口,并由Nginx进行代理。请确保在配置和使用过程中注意安全性和网络设置,并根据您的需求进行相应调整。
41 0
|
4月前
|
应用服务中间件 nginx Python
nginx代理目录
nginx代理目录
47 1