1. 程式人生 > 實用技巧 >配置nginx 反向代理

配置nginx 反向代理

新專案整合公司統一的SSO登陸,登陸域名aaa。前端啟動服務localhost:10002,自動跳轉到登陸地址,登陸成功後會進行跨域訪問。

解決方案:nginx 反向代理
1.安裝nginx:

brew install nginx 如果遇到update Homebrew..., control+C終止當前程序,開始下一程序安裝。

2.安裝成功後,啟動sudo nginx或者brew services start nginx,,訪問http://localhost:8080/檢視是否啟動成功

3.配置nginx:

cd /usr/local/etc/

cd nginx

cat nginx.conf

讀取servers 資料夾下的配置檔案。

沒有servers資料夾我們自己建立:

mkdir servers

cd servers

touch cld.conf 建立cld.conf 配置檔案

vim cld.conf 編輯配置

當瀏覽器訪問cld.aaa.com 時,將會指向本地服務

server {
    listen  80;
    server_name  cld.aaa.com;
      location ~ ^\/login$ {
         rewrite ^\/login$ /api/user/login break;
         proxy_pass http://max-auth-new.aaa.com;
proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } location / { proxy_pass http://localhost:10002/; 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_set_header x-request-id $request_id; add_header Cache-Control no-cache; add_header Pragma no-cache; add_header Expires 0; if ($request_filename ~* .*\.(?:js|css)$) { expires 7d; } } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }

4.重啟nginx:

sudo nginx -s reload

5.使用switchhost修改本地host ip和域名對映

127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost
127.0.0.1	cld.aaa.com