nginx session丢失&域名去根&反相代理

项目情况

典型maven项目分模块管理,项目包名  后台:admin   前台:front 

需求 

1.部署时可以根据 形如:http://www.xxx.com 直接可以访问到网站首页。

2.不能通过 形如 http://www.xxx.com:8080/front 访问(通常开发的时候都是http://localhost:8080/front 访问)。

3.使用nginx 负载均衡,反向代理。

4.上传的图片,文件需要做静态分离 甚至js,css等。

5.最好少改动项目代码或配置文件。


简单介绍安装

首先到官网下载nginx ,有对windows,Linux,OS X Yosemite 等系统的支持(本人用的是Macbook Pro 开发过程也一直使用着nginx1.6.x)

安装的话 windows 直接下载 解压后即可运行。

Macbook 系统是 Unix  安装传送门:http://blog.csdn.net/dracotianlong/article/details/21817097。

Linux 指令和Unix 差不多 安装传送门: http://www.cnblogs.com/kunhu/p/3633002.html。

根据项目情况修改配置文件 各个系统的 nginx.conf 文件除了 系统盘符路径不样做修改后是通用的。

常用的指令

windows  :  

启动 :nginx

重新加载配置文件:nginx -s reload (前提是nginx启动的情况下) 

停止 :nginx -s stop 

或者直接进入到 nignx根目录 双击 绿色的nginx.exe ,关闭也直接可以打开任务管理器把进程结束掉(记得要结束2个进程)

Macbook :

启动:sudo nginx   然后是电脑密码(我的一直就要输这个密码)

重新加载配置文件:sudu nignx -s relaod

停止:sudo nginx -s stop

Linux:

启动:./nginx  

重新加载配置文件:nignx -s relaod

停止:nginx -s stop  或找到pid kill掉


配置

反向代理

 location / {

	proxy_pass http://localhost:8080/front/;
				
 }

分离图片,文件库(需要做指定上传到imgbase 处理,统一imgbase下)

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/imgbase;
           index  index.html index.htm;
       }
	
    }

网站静态资源css,js,png ,gif等等

 location /front/res {
        alias /Users/front/src/main/webapp/res;	 #css,js 等资源
   }
	
 location /front/images {
        alias /Users/front/src/main/webapp/res/images; #png ,gif等资源       
   }

反向代理session丢失处理

注意下面的  proxy_set_header Cookie $http_cookie; (设置Cookie) 

以及 proxy_cookie_path /front/  /;  proxy_cookie_path /front  /;(设置cookes的路径转换 

例如我的是:proxy_pass http://localhost:8080/front/;   相应的转换就是 proxy_cookie_path /front/ /;)

location / {
      proxy_redirect off;   
      proxy_set_header Host $host;
      <span style="color:#ff6666;">proxy_set_header Cookie $http_cookie;</span>   
      proxy_set_header X-Real-IP $remote_addr;   
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://localhost:8080/front/;
      <span style="color:#ff6666;">proxy_cookie_path /front/ /;</span>
      <span style="color:#ff6666;">proxy_cookie_path /front /;</span>
 }

完整配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8090;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/imgbase;
           index  nopic.png;
       }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /front/res {
           	alias /Users/front/src/main/webapp/res;	
	}
	
	location /front/images {
                alias /Users/front/src/main/webapp/res/images;          
        }	
		
        location / {
        	proxy_redirect off;   
		proxy_set_header Host $host;
		proxy_set_header Referer $http_referer;
		proxy_set_header Cookie $http_cookie;   
		proxy_set_header X-Real-IP $remote_addr;   
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://localhost:8080/front/;
		proxy_cookie_path /front/ /;
		proxy_cookie_path /front /;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }


}

访问效果

eclipse 启动应用后 访问应用有二种方法

方法一:按开发模式   http://localhost:8080/front

方法 二:按发布模式   http://localhost:81

两种方法的效果一样,若如果是80端口的话  直接 http://localhost 既可满足  ip绑定域名后 http://www.xxx.com

否则需要把项目重新拷贝到tomcat  webapps下root ,修改配置代码等








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值