Nginx与Nancy 504超时错误

时间:2017-11-01 17:06:56

标签: nginx reverse-proxy nancy

我正在与Nancy一起实施.NET Core的Hellow World项目。它运行良好,我可以在本地运行时使用该服务,如下所示。

administrator@NewNancyServer:~$ curl http://localhost:5000
Hello from Nancy running on CoreCLR

然而,当尝试使用Nginx反向代理时,即使我可以看到" nginx欢迎屏幕"在端口80并且服务器正在运行时,它始终返回 504超时错误

注意:我能够看到端口正在使用 netstat 进行收听,并且所有日志仅显示"超时&#34 ;

2017/11/01 14:57:09 [error] 4411#4411: *18 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "localhost:100"

下面,我将添加我的服务器配置。

我正在使用:

  • 主管3.2.0
  • NGinx 1.10.3
  • Ubuntu Server 16.04.3
  • .Net core 2.0.2和1.0.4已安装

Nginx配置

nginx.conf



user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}




Nancydemo

文件位于"网站已启用"



server {
    listen       100;


    location / {
            proxy_pass http://127.0.0.1:5000;
    }
}




修改

正如@Lundberg所说,这是我的netstat输出。看起来它正在侦听IPv6和IPv4



root@NewNancyServer:/var/log/nginx# netstat -l -t -n | grep -E "(100|5000)"
tcp        0      0 127.0.0.1:5000          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:100             0.0.0.0:*               LISTEN
tcp6       0      0 ::1:5000                :::*                    LISTEN




经过大量时间研究这个问题后,我遇到了一个Microsoft文档来解决使用Kestrel配置的Nginx问题。潜入其中,一个新的"网站启用"显示了配置,只添加了一些配置行使其完美运行!

该文件现在看起来像

Nancydemo

文件位于"网站已启用"



server {
    listen       100;
    location / {
            proxy_pass http://127.0.0.1:5000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }
}




更有趣的配置和完整的分步可以在https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction?tabs=aspnetcore1x

找到

1 个答案:

答案 0 :(得分:0)

我的直觉:这可能与IPv4与IPv6有关。以下是我的服务器上的示例输出:

per@some-server:~$ netstat -l -t -n Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 127.0.0.1:10024 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:113 0.0.0.0:* LISTEN tcp 0 0 10.8.0.1:53 0.0.0.0:* LISTEN tcp 0 0 88.80.6.225:53 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:953 0.0.0.0:* LISTEN tcp6 0 0 :::993 :::* LISTEN tcp6 0 0 :::143 :::* LISTEN tcp6 0 0 :::53 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 :::25 :::* LISTEN tcp6 0 0 ::1:953 :::* LISTEN

tcp行正在侦听标准IPv4 TCP端口,tcp6正在侦听IPv6。如果服务仅 侦听IPv6并且客户端尝试使用IPv4进行连接,则会中断(反之亦然。)

你能做netstat -l -t -n | grep -E "(100|5000)"并与我们分享结果吗?我有时会看到localhost的奇怪行为,某些服务只能使用IPv6或类似服务。 可能是Nancy进程只侦听IPv4,但是主机启用了IPv6并且nginx尝试通过IPv6或类似的东西访问上游......