設定 nginx 作為 kestrel 的代理伺服器 (ASP.Net Core)

設定 nginx 作為 kestrel 的代理伺服器 (ASP.Net Core)

我正在嘗試設定 ASP.NET Core 環境來託管我的應用程序,但我在使用 nginx Web 伺服器時遇到了問題。當我嘗試連接到我的網域時,我得到了502 Bad Gateway. Nginx 應該只作為 kestrel 的代理伺服器運作。

這裡這是我正在關注的指南的連結。該配置幾乎是微軟建議的配置,我只是更改了每個環境不同的變數。

有問題的行位於http://aspdotnethost;nginx.conf 配置的末尾。當我將其註解掉時,我將被重定向到預設的 www 位置。

那麼該線路實際上是如何工作的以及如何管理它以正確重定向到 localhost:5000 ?

/etc/nginx/proxy.conf

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_set_header    X-Forwarded-Proto $scheme;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffers           32 4k;

/etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    include    /etc/nginx/proxy.conf;
    limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
    server_tokens off;

    sendfile on;
    keepalive_timeout 29; # Adjust to the lowest possible value that makes sense for your use case.
    client_body_timeout 10; client_header_timeout 10; send_timeout 10;

    upstream aspdotnethost {
        server localhost:5000;
    }

    server {
        listen *:80;
        add_header Strict-Transport-Security max-age=15768000;
        return 301 https://$host$request_uri;
    }

    server {
        listen *:443    ssl;
        server_name     example.com *.example.com;

        #Redirects all traffic
        location / {
            proxy_pass  http://aspdotnethost;
            limit_req   zone=one burst=10;
        }
    }

}

Nginx 設定檢查:

[root@rs-zap353479-1 patrick]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

當我輸入時curl http://localhost:5000,我會返回 html 文件。所以這個組件應該在我的眼中找到。

答案1

就像 Eugen Rieck 作為評論發表的那樣,我必須向/etc/hosts.

作為附加步驟,我必須proxy_temp_path在 中指定proxy.conf,建立該路徑並將其 chown 給正在執行 nginx 的使用者(在我的例子中nginx)。

相關內容