跳到主內容

Nginx 反向代理

server {
    listen       80;
    listen  [::]:80;
    server_name  www.srou.com;

    access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        # proxy_pass http://10.2.99.14:8081;
        index  index.html index.htm;
    }

    location /dev/ {
        proxy_pass http://10.2.99.14:8081;    
    }


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

server {
    listen       80;
    listen  [::]:80;
    server_name  wsf.srou.com;

    location / {
        # root   /usr/share/nginx/html;
        proxy_pass http://10.2.99.14:8081;
        index  index.html index.htm;
    }

    location /uat/ {
        proxy_pass http://10.2.99.14:8082;    
    }

}

server {
    listen       80;
    listen  [::]:80;
    server_name  wsm.srou.com;

    location / {
        # root   /usr/share/nginx/html;
        proxy_pass http://10.2.99.14:8082;
        index  index.html index.htm;
    }
}


使用 IP 來替代域名的配置方法

  1. 查找域名對應的 IP 地址:確保你知道 a.aaa.com 對應的 IP 地址。

  2. 修改 Nginx 配置:將原本指向域名的 proxy_pass 修改為指向 IP 地址。

具體操作步驟:

假設 aaa.aaa.com 對應的 IP 地址是 192.168.1.1,這樣配置:

location / {
    proxy_pass https://192.168.1.1;
    proxy_ssl_name a.aaa.com;  # 設置 SSL 握手時使用的域名
    proxy_ssl_server_name on;    # 開啟 SNI 支持
}

配置解釋:

  1. proxy_pass 使用 IP 地址:直接使用 IP 地址(如 https://192.168.1.1)來替代域名,這樣就不需要 DNS 解析。

  2. proxy_ssl_name 指令:即使使用了 IP 地址,你仍然需要指示 Nginx 在 SSL 握手過程中使用哪個主機名。這是因為 SSL/TLS 握手過程通常依賴於域名,特別是在有 SNI(Server Name Indication)需求的情況下。

  3. proxy_ssl_server_name on 指令:這個指令開啟 SNI 支持,以便在 SSL 握手時能夠發送正確的域名信息給後端服務器。

不使用 DNS 的注意事項

  • 如果你不使用 DNS 而選擇 IP 地址,一旦服務器 IP 發生變化,你需要手動更新 Nginx 配置中的 IP 地址。

  • 使用 IP 地址的配置需要確保 IP 對應的服務器上有正確的 SSL 憑證,並且該憑證包含了你在 proxy_ssl_name 指令中指定的域名。

這種方法有效避免了 Nginx 對 DNS 解析的依賴,但需要確保 IP 地址和 SSL 配置的正確性。