【问题标题】:ERR_CONNECTION_REFUSED NGINX PROXY REVERSEERR_CONNECTION_REFUSED NGINX 代理反向
【发布时间】:2021-10-24 07:46:25
【问题描述】:

我在解决几天内无法解决的问题时遇到了困难。

问题是ERR_CONNECTION_REFUSED的错误。

我正在尝试使用子域通过反向代理访问 api。

我正在使用 Ubuntu。

这里是配置:

default:

    server {
    listen 80;
    listen [::]:8080;
    root /var/www/servicos.siteabc.org;
    server_name  servicos.siteabc.org;
    #server_name _ ;
     access_log /var/log/nginx/access.log;
     error_log /var/log/nginx/error.log;
    location / {
                proxy_redirect off;
                proxy_set_header host $host;
                proxy_set_header X-real-ip $remote_addr;
                proxy_set_header X-forward-for $proxy_add_x_forwarded_for;
                proxy_pass http://localhost:5000;
    }
}

servicos.siteabc.org:

        [Unit]
    Description=Example .NET Web API App running on Ubuntu
    [Service]
    WorkingDirectory=/var/www/servicos.siteabc.org
    ExecStart=/usr/bin/dotnet /var/www/servicos.siteabc.org/TDWebApi.dll
    Restart=always
    # Restart service after 10 seconds if the dotnet service crashes:
    RestartSec=10
    KillSignal=SIGINT
    SyslogIdentifier=dotnet-example
    #User=root;
    Environment=ASPNETCORE_ENVIRONMENT=Production
    Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
    [Install]
    WantedBy=multi-user.target

nginx.conf

        user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    include /etc/nginx/modules-enabled/*.conf;
    
    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 TLSv1.3; # 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_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/*;
            client_max_body_size 10M;
    }

显然我的服务运行正常:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-10-22 17:32:00 UTC; 1h 21min ago
       Docs: man:nginx(8)
    Process: 275411 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
   Main PID: 273065 (nginx)
      Tasks: 3 (limit: 614)
     Memory: 10.3M
     CGroup: /system.slice/nginx.service
             ├─273065 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─275412 nginx: worker process
             └─275413 nginx: worker process

Oct 22 17:32:00 siteabc.org systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 22 17:32:00 siteabc.org systemd[1]: Started A high performance web server and a reverse proxy server.
Oct 22 18:21:19 siteabc.org systemd[1]: Reloading A high performance web server and a reverse proxy server.
Oct 22 18:21:19 siteabc.org systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Oct 22 18:23:21 siteabc.org systemd[1]: Reloading A high performance web server and a reverse proxy server.
Oct 22 18:23:21 siteabc.org systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Oct 22 18:25:44 siteabc.org systemd[1]: Reloading A high performance web server and a reverse proxy server.
Oct 22 18:25:44 siteabc.org systemd[1]: Reloaded A high performance web server and a reverse proxy server.
Oct 22 18:30:51 siteabc.org systemd[1]: Reloading A high performance web server and a reverse proxy server.
Oct 22 18:30:51 siteabc.org systemd[1]: Reloaded A high performance web server and a reverse proxy server.
root@siteabc:/var/www# sudo systemctl status servicostreed.service
● servicostreed.service - Example .NET Web API App running on Ubuntu
     Loaded: loaded (/etc/systemd/system/servicostreed.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-10-22 16:59:37 UTC; 1h 53min ago
   Main PID: 271237 (dotnet)
      Tasks: 16 (limit: 614)
     Memory: 34.9M
     CGroup: /system.slice/servicostreed.service
             └─271237 /usr/bin/dotnet /var/www/servicos.siteabc.org/TDWebApi.dll

Oct 22 16:59:37 siteabc.org systemd[1]: Started Example .NET Web API App running on Ubuntu.
Oct 22 16:59:39 siteabc.org dotnet-example[271237]: info: Microsoft.Hosting.Lifetime[0]
Oct 22 16:59:39 siteabc.org dotnet-example[271237]:       Now listening on: http://localhost:5000
Oct 22 16:59:39 siteabc.org dotnet-example[271237]: info: Microsoft.Hosting.Lifetime[0]
Oct 22 16:59:39 siteabc.org dotnet-example[271237]:       Application started. Press Ctrl+C to shut down.
Oct 22 16:59:39 siteabc.org dotnet-example[271237]: info: Microsoft.Hosting.Lifetime[0]
Oct 22 16:59:39 siteabc.org dotnet-example[271237]:       Hosting environment: Production
Oct 22 16:59:39 siteabc.org dotnet-example[271237]: info: Microsoft.Hosting.Lifetime[0]
Oct 22 16:59:39 siteabc.org dotnet-example[271237]:       Content root path: 

【问题讨论】:

    标签: ubuntu nginx reverse-proxy nginx-reverse-proxy


    【解决方案1】:

    error CONNECTION_REFUSED 适用于服务未在您请求的特定端口上运行的情况。使用命令ss -nltup 获取正在侦听的所有可用端口的列表。我认为该服务未在端口 5000 上运行。

    【讨论】:

    • 我不明白。看起来很正常:tcp LISTEN 0 128 127.0.0.1:5000 0.0.0.0:* users:(("dotnet",pid=271237,fd=180)) tcp LISTEN 0 128 [::1]:5000 [: :]:* 用户:(("dotnet",pid=271237,fd=181))
    • nginx 服务和 .net 应用程序都在同一台机器上运行。并且根据您上面发布的内容,您没有使用容器,因为您使用的是 systemd,所以请在机器上的端口 5000 上执行 telnet,以检查您的 .net 应用程序是否可用。您可能会注意到您的 .net 应用程序正在侦听 127.0.0.1,这意味着它只能在您的机器上使用,而不是在它之外。
    • 如何检查?
    • 从你有 nginx 的服务器,执行以下命令:telnet localhost 5000 也最好检查你的 nginx 日志。
    • 我设法解决了它。我打开 proxy_pass 到 https 127.0.0.1:5001。端口 5000 即使使用 http 也不起作用我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 2011-08-09
    • 2013-02-18
    • 2021-04-08
    • 2019-03-19
    • 2016-07-22
    相关资源
    最近更新 更多