当您不使用nginx作为代理服务器时,可以正常访问登录界面,并且可以传递cookie。如下图所示 我的真实服务器的域名为http://bdp106.bitnei.net:9763,代理服务器nginx为http://bdp106.bitnei.net:81。通过nginx进行访问时,浏览器不会在跨域后执行Cookie。如下图所示,每次登录到登录界面时,都会生成一个新的cookie,导致登录失败。 enter image description here
以下是我的nginx配置:
#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" "$http_cookie"';
upstream tomcat_01 {
server 192.168.6.106:9763 max_fails=3 fail_timeout=600;
}
upstream tomcat_02 {
server 192.168.6.106:9763 max_fails=3 fail_timeout=600;
server 192.168.6.106:9763 max_fails=3 fail_timeout=600;
}
upstream default {
server 192.168.6.106:9763 max_fails=3 fail_timeout=600;
}
server {
listen 81;
server_name bdp.bitnei.net *.bitnei.net;
access_log /home/bianqi/log/access.log main;
#页面访问
location /static{
proxy_pass http://tomcat_02/static/;
proxy_redirect off;
proxy_cookie_domain nginx.server bdp106.bitnet.net;
proxy_cookie_path /bdp /static;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Headers Content-Type;
add_header Access-Control-Allow-Methods GET,POST;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers 'Cookie,Set-Cookie,x-requested-with,content-type';
proxy_ignore_headers "Set-Cookie";
}
#接口服务
location ^~ /bdp/ {
rewrite ^(bdp/?.*)$ /$1 break;
proxy_pass http://tomcat_02/bdp/;
proxy_cookie_domain nginx.server bdp106.bitnet.net:9763;
proxy_cookie_path /bdp /bdp;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Headers Content-Type;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Headers 'Cookie,Set-Cookie,x-requested-with,content-type';
proxy_ignore_headers "Set-Cookie";
}
#静态资源
location ~* \.(png|gif|css|js|svg|jpg|jpeg|svg)$ {
rewrite ^/(.*) http://192.168.6.103:9763/$1 permanent;
}
location /{
add_header Access-Control-Allow-Origin *;
proxy_cookie_domain nginx.server bdp106.bitnet.net;
add_header Access-Control-Allow-Headers Content-Type;
add_header Access-Control-Allow-Methods GET,POST;
add_header Access-Control-Allow-Credentials true;
proxy_ignore_headers "Set-Cookie";
}
}
#include /home/bianqi/nginx1/nginx-1/conf/nginx_ssl.conf;
}
如何保证cookie传输一致并且登录成功!