您的位置:首页 > 运维架构 > Nginx

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

2015-06-02 21:27 489 查看
项目情况

典型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 ,修改配置代码等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: