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访问新页面而且地址栏地址不变