asp.net-core - 无法使用 nginx 访问托管在 ubuntu 上的网站

标签 asp.net-core ubuntu nginx

操作系统:Ubuntu 18.04,Nginx 版本:1.14.0,Asp.net 版本:core 3.1
长话短说:

  • 无法从 linux 机器外部访问端口 5001。但是可以打到里面。
  • Nginx 正在通过请求,但它没有命中网络应用程序。
  • 同一 nginx 下同一服务器上的另一个网站正在运行。

  • 部署后它停止工作。当我访问 https://example.com , Nginx 发出 307 重定向到 https://www.example.com:5001/ .我的浏览器无法访问它,说“看起来 www.mywsite.com 关闭了连接”。部署只改变了
    wget https://localhost:5001 --no-check-certificate
    
    因此,Web 应用程序似乎会响应对端口 5001 发出的请求。但不知何故,请求并没有从 Nginx 发送到应用程序。
    另一个托管在同一服务器上的网站(监听端口 5050)与 Nginx 几乎相同的配置在浏览器上运行良好(接收代码 200 而不是 307)。
    我已经重新启动了网络服务器,重新启动了 dotnet Web 应用程序,重新加载了 nginx。没有任何效果。
    这是“mysite”的 nginx 配置:
    server {
        
        large_client_header_buffers 4 32k;
    
        server_name example.com www.example.com;
    
        location / {
            proxy_pass  http://mysite;
            proxy_http_version 1.1;
            root /var/www/mysite/;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded_Proto $scheme;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    }
    
    upstream mysite{
        zone dotnet 64k;
        server 127.0.0.1:5000;
    }
    
    server {
        if ($host = www.example.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        if ($host = example.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    large_client_header_buffers 4 32k;
    
        listen *:80;
    
        server_name example.com www.example.com;
        return 404; # managed by Certbot
    
    }
    
    当我通过 dotnet 列出所有监听服务时,有趣的是工作网站只监听 5050:

    tcp 0 0 127.0.0.1:5050 0.0.0.0:* LISTEN 1654/dotnet

    tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 5510/dotnet

    tcp 0 0 127.0.0.1:5001 0.0.0.0:* LISTEN 5510/dotnet

    tcp 0 0 127.0.0.1:40678 127.0.0.1:3306 ESTABLISHED 1654/dotnet

    tcp6 0 0 ::1:5050 :::* LISTEN 1654/dotnet

    tcp6 0 0 ::1:5000 :::* LISTEN 5510/dotnet

    tcp6 0 0 ::1:5001 :::* LISTEN 5510/dotnet


    来自 nginx access.log 的尾部:

    (my IPv4 address) - - [05/May/2021:15:37:55 -0400] "GET / HTTP/1.1" 307 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46"


    来自 nginx error.log 的尾部(我确实找到了几个小时前的一些条目,但最近的失败没有创建条目):

    2021/05/05 12:03:11 [error] 5481#5481: *187 upstream prematurely closed connection while reading response header from upstream, client: {my IPv4 address} 26, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5001/", host: "example.com"


    任何帮助表示赞赏。
    谢谢你。

    最佳答案

    在您的配置中,我只看到您设置监听指令的 3 个区域。首先,每个默认“监听 443 ssl”的所有接口(interface)上的 443 glob。然后你指示它在端口 5000 上监听 127.0.0.1,然后你在端口 80 上进行 glob。在这一切中,你都没有告诉它在端口 5001 上监听,但也许 nginx 会自动执行此操作?如果是这样,那么您的“listen 127.0.0.1:5000”很可能应该是“listen *:5000”,因此它会在您的所有接口(interface)(ipv4 和 ipv6)上出现。

    关于asp.net-core - 无法使用 nginx 访问托管在 ubuntu 上的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67407115/

    相关文章:

    regex - 使用 nginx 重定向捕获的正则表达式组

    ubuntu - 部署后如何强制 PM2 更新我的 meteor 应用程序服务器端?

    c# - StackExchange.Redis:没有可用的连接服务此操作。无法在HOST:PORT上连接/交互

    asp.net-core - 我可以将 asp.net core mvc 和 web api 作为单个项目吗

    linux - Linux 是否支持不需要安装的独立软件包/软件?

    java - 无法在hadoop中使用dfs创建目录

    c# - Ubuntu终端上的.NET Core参数十进制错误

    c# - ASP.NET Core禁用json补丁

    c# - ASP.NET Core EF Code First appsettings.json

    nginx - 是否可以在 NGINX 中合并多个响应并发送一个响应