首页 > 开发 > Nginx > 正文

利用nginx解决cookie跨域访问的方法

2020-07-28 15:39:36
字体:
来源:转载
供稿:网友
本篇文章主要介绍了利用nginx解决cookie跨域访问的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

一、写在前面

最近需要把阿里云上的四台服务器的项目迁移到客户提供的新的项目中,原来的四台服务器中用到了一级域名和二级域名。比如aaa.abc.com 和bbb.abc.com 和ccc.abc.com。其中aaa.abc.com登录,通过把cookie中的信息setDomain给.abc.com。其他系统可以共享这个cookie。但是新的四台服务器中并没有申请域名,只有四个ip:

192.168.0.1    单点登录服务器

192.168.0.2

192.168.0.3

192.168.0.4

因为每台服务器有两个项目,都用到单点登录,所以通过修改新的共享登录方式花费时间太多,于是在网上搜cookie的跨域登录,尝试了下,在192.168.0.1    单点登录服务器中多次setDomain分别给2、3、4服务器,结果不理想,因为浏览器不允许。后来无意中看到nginx可以通过欺骗的方式共享cookie。于是想到原来公司部署nginx还有这层用法。

二、原来的nginx配置

先说下nginx的安装,这个网上都有很多教程,不在赘述,我是参照于在Linux里安装、启动nginx。需要注意的是./configure后面的各种with,我在配置启动过程遇到了一些问题:

nginx: [emerg] unknown directive "aio" in

加上--with-file-aio 

复制代码 代码如下:Starting nginx: nginx: [emerg] the INET6 sockets are not supported on this platform in “[::]:80” of the

在后面加上--with-ipv6好使。

安装完成后。主要是nginx.conf的配置

原来服务器的配置nginx.conf:

# For more information on configuration, see:#  * Official English Documentation: http://nginx.org/en/docs/#  * Official Russian Documentation: http://nginx.org/ru/docs/user root;worker_processes 2;worker_cpu_affinity 1000 0100;error_log logs/error.log;pid logs/nginx.pid;events {  worker_connections 2048;}http {  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;  gzip on;  gzip_min_length 1000;  gzip_buffers   4 8k;  gzip_types    text/plain application/javascript application/x-javascript text/css application/xml;  client_max_body_size 8M;  client_body_buffer_size 128k;  sendfile      on;  tcp_nopush     on;  tcp_nodelay     on;  keepalive_timeout  65;  types_hash_max_size 2048;  include       mime.types;  default_type    application/octet-stream;  connection_pool_size 512;  aio on;  open_file_cache max=1000 inactive=20s;  # Load modular configuration files from the /etc/nginx/conf.d directory.  # See http://nginx.org/en/docs/ngx_core_module.html#include  # for more information.  #  主要配置在这里,nginx.conf配置都是一样  include /usr/local/nginx/conf/conf.d/*.conf;  server {    listen    80 default_server;    listen [::]:80 ipv6only=on default_server;    server_name _;    root     html;    # Load configuration files for the default server block.    include /usr/local/nginx/conf/default.d/*.conf;    location / {    }    error_page 404 /404.html;      location = /40x.html {    }    error_page 500 502 503 504 /50x.html;      location = /50x.html {    }  }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表