從根本解決跨域(nginx部署解決方案)

配置本地host
···
127.0.0.1 rd.max.com
127.0.0.1 fe.max.com
···html

配置nginx.conf前端

upstream jianshubao-rd {
    server 192.168.0.102:8082;
    }
upstream jianshubao-fe {
    server 192.168.0.103:8080;
    }
server {
        listen       80;
        server_name  fe.max.com;

        location / {
            proxy_pass   http://jianshubao-fe;
            index  index.html index.htm;
        }
        location /fe {
            rewrite ^.+fe/?(.*)$ /$1 break;
            #proxy_pass   http://jianshubao-fe;
            proxy_pass   http://localhost:82;
            index  index.html index.htm;
        }    


    }
server {
        listen       82;
        server_name  rd.max.com;

            location / {
            proxy_pass   http://jianshubao-rd;
            index  index.html index.htm;
            }
        }

反向代理的職責就是用戶不知道訪問的是哪臺服務器nginx

經過監聽80端口,將/fe後綴的請求重定向到本地的82端口
經過upstream定義 讓兩臺主機的server重定向到一臺主機的80端口 和82端口上
進而實現先後端分離,先後端又在一個域下面
可是這樣使用起來產生了一個新的問題,前端ajax請求後端的時候響應速度慢了不少。
有多是 百兆網卡的問題,還有多是配置的不是很完滿。
不過先實現再優化是咱們的開發原則。ajax

相關文章
相關標籤/搜索