之前一直听说nginx的代理模式,但一直没有实际接触,但是最近项目需要将三个服务公用一个登陆系统,也就是说只要在一个服务器里登陆,其他服务器不需要再登陆了,有人说用单点登陆,但是实际情况用单点登陆改的代码很多,通过大神指点可以用nginx代理,可以共用同一个cookie从而实现。
不同的端口所保存的cookie是不能通用的,只有通过nginx代理。原理就是当三个服务端口为8080,8081,8082,而配置nginx端口为88;看配置
server {
listen 88;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /server2/{
proxy_pass http://127.0.0.1:8082/server2/;
}
location /server1/ {
proxy_pass http://127.0.0.1:8081/server1/;
}
location /server/ {
proxy_pass http://127.0.0.1:8080/server/;
}
#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 D:\wwwroot;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
需要注意的是localhost后面需要固定服务的地址前缀,server1,这样访问时可以直接访问:http://127.0.0.1:88/servce1/login.html,这样就能直接访问到端口为8081的服务。大家可以尝试配置一下
在附上nginx解决跨域问题的配置
http {
include mime.types;
default_type application/octet-stream;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
.
.
.
}