本文小編為大家詳細(xì)介紹“如何利用nginx解決cookie跨域訪問的問題”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“如何利用nginx解決cookie跨域訪問的問題”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比綠園網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式綠園網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋綠園地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
一、寫在前面
最近需要把阿里云上的四臺(tái)服務(wù)器的項(xiàng)目遷移到客戶提供的新的項(xiàng)目中,原來的四臺(tái)服務(wù)器中用到了一級(jí)域名和二級(jí)域名。比如aaa.abc.com 和bbb.abc.com 和ccc.abc.com。其中aaa.abc.com登錄,通過把cookie中的信息setdomain給.abc.com。其他系統(tǒng)可以共享這個(gè)cookie。但是新的四臺(tái)服務(wù)器中并沒有申請域名,只有四個(gè)ip:
192.168.0.1 單點(diǎn)登錄服務(wù)器
192.168.0.2
192.168.0.3
192.168.0.4
因?yàn)槊颗_(tái)服務(wù)器有兩個(gè)項(xiàng)目,都用到單點(diǎn)登錄,所以通過修改新的共享登錄方式花費(fèi)時(shí)間太多,于是在網(wǎng)上搜cookie的跨域登錄,嘗試了下,在192.168.0.1 單點(diǎn)登錄服務(wù)器中多次setdomain分別給2、3、4服務(wù)器,結(jié)果不理想,因?yàn)闉g覽器不允許。后來無意中看到nginx可以通過欺騙的方式共享cookie。于是想到原來公司部署nginx還有這層用法。
二、原來的nginx配置
先說下nginx的安裝,這個(gè)網(wǎng)上都有很多教程,不在贅述,我是參照于在linux里安裝、啟動(dòng)nginx。需要注意的是./configure后面的各種with,我在配置啟動(dòng)過程遇到了一些問題:
nginx: [emerg] unknown directive "aio" in
加上--with-file-aio
復(fù)制代碼 代碼如下:
starting nginx: nginx: [emerg] the inet6 sockets are not supported on this platform in “[::]:80” of the
在后面加上--with-ipv6好使。
安裝完成后。主要是nginx.conf的配置
原來服務(wù)器的配置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 { } } }
原來服務(wù)器的
conf.d/*.conf的配置是reverse-proxy.conf
server { listen 80; server_name m.abc.com.cn; location / { root /usr/share/nginx/html/; index index.html index.htm; } location ~ \.(jsp|do)?$ { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://localhost:8084; } if ($http_user_agent ~* "qihoobot|baiduspider|googlebot|googlebot-mobile|googlebot-image|mediapartners-google|adsbot-google|feedfetcher-google|yahoo! slurp|yahoo! slurp china|youdaobot|sosospider|sogou spider|sogou web spider|msnbot|ia_archiver|tomato bot") { return 403; } access_log /home/logs/nginx/m.abc.com.cn_access.log; } server { listen 80; server_name store.abc.com.cn *.store.abc.com.cn; location / { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://localhost:8081; } access_log /home/logs/nginx/store.abc.com.cn_access.log; } server { listen 80; server_name shopcenter.abc.com.cn; location / { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://10.45.100.222:8082; } access_log /home/logs/nginx/shopcenter.abc.com.cn_access.log; } server { listen 80; server_name search.abc.com.cn; location / { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://10.45.100.68:8083; } access_log /home/logs/nginx/search.abc.com.cn_access.log; }
以上配置后,nginx啟動(dòng)后,通過訪問不同的域名來訪問不同服務(wù)器。而因?yàn)槎加卸?jí)域名.abc.com.cn。所以可以共享cookie。
nginx的文件結(jié)構(gòu)為:
三、修改后的nginx配置
主要是reverse-proxy.conf 不同
server { listen 9998; server_name 192.168.0.1:9998; location /servlets/ { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://192.168.0.1:8088; } location / { root /usr/local/nginx/html/web/; index index.html index.htm; } location ~ \.(jsp|do)?$ { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://192.168.0.1:8088; proxy_http_version 1.1; proxy_set_header upgrade $http_upgrade; proxy_set_header connection "upgrade"; proxy_read_timeout 700s; } if ($http_user_agent ~* "qihoobot|baiduspider|googlebot|googlebot-mobile|googlebot-image|mediapartners-google|adsbot-google|feedfetcher-google|yahoo! slurp|yahoo! slurp china|youdaobot|sosospider|sogou spider|sogou web spider|msnbot|ia_archiver|tomato bot") { return 403; } access_log /usr/local/nginx/logs/www.abc.com.cn_access.log; } server { listen 9994; server_name 192.168.0.1:9994; location / { proxy_redirect off; root /usr/local/nginx/html/weixin/; index index.html index.htm; } location ~ \.(jsp|do)?$ { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://localhost:8084; } if ($http_user_agent ~* "qihoobot|baiduspider|googlebot|googlebot-mobile|googlebot-image|mediapartners-google|adsbot-google|feedfetcher-google|yahoo! slurp|yahoo! slurp china|youdaobot|sosospider|sogou spider|sogou web spider|msnbot|ia_archiver|tomato bot") { return 403; } access_log /usr/local/nginx/logs/m.abc.com.cn_access.log; } server { listen 9990; server_name store.abc.com.cn *.store.abc.com.cn; location / { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://localhost:8081; } access_log /usr/local/nginx/logs/store.abc.com.cn_access.log; } server { listen 9992; server_name 192.168.0.1:9992; location / { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://192.168.0.2:8082; } access_log /usr/local/nginx/logs/shopcenter.abc.com.cn_access.log; } server { listen 9993; server_name 192.168.0.1:9993; location / { proxy_redirect off; proxy_set_header host $host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://192.168.0.3:8083; } access_log /usr/local/nginx/logs/search.abc.com.cn_access.log; }
這樣就可以把192.168.0.1:9998 當(dāng)做單點(diǎn)服務(wù)器,登錄后的domain都為192.168.0.1 。其他的0.2、0.3都可以通過192.168.0.1nginx和單點(diǎn)服務(wù)器的不同端口訪問,那么就可以共享這個(gè)0.1的域名了。
讀到這里,這篇“如何利用nginx解決cookie跨域訪問的問題”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標(biāo)題:如何利用nginx解決cookie跨域訪問的問題
網(wǎng)站鏈接:http://www.rwnh.cn/article24/jeesje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、服務(wù)器托管、搜索引擎優(yōu)化、網(wǎng)站排名、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)