3.Nginx與阿里雲二級域名配合做反向代理

背景需求:
存在多個服務,希望藉助阿里雲的二級域名,比如原始域名爲http://zbomc-jack.com/
我希望加上二級域名,admin.zbomc-jack.com或者web.zbomc-jack.com,通過這個地址
判斷前臺調用不同的後臺服務

阿里雲配置

1.設置二級域名

域名-域名列表->一級域名點擊解析設置
在這裏插入圖片描述

2.Nginx解析以及反向代理配置

# 負載均衡配置
upstream userService{
    server zbomc-jack.com:29996 weight=1;
}
server {

    #解決跨域帶來的預請求問題
    if ($request_method = OPTIONS){
        return 200;
    }

    #監聽http端口號及域名
    listen       80;
    server_name  admin.zbomc-jack.com;

    location / {
        proxy_set_header Host $host;
        # 根據自己實際情況設置成前端代碼地址
        root    /usr/share/nginx/html;
        index   index.html;
        # 解決vue的histry路由模式問題
        try_files $uri $uri/ =404;
    }

    # 反向代理後臺url地址
    location /user {
        proxy_pass http://userService;
        # 啓用keep alive
        proxy_http_version 1.1;
        proxy_set_header Connection "";

        # 獲取 xforward和真實IP
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  Host $host;
    }

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {#使用nginx默認的500頁面
        root   /usr/share/nginx/html;
    }
}

3.重啓nginx並驗證

nginx -t 
nginx -s reload
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章