「502 Bad Gateway」で nginx の Web サイトにアクセスする

「502 Bad Gateway」で nginx の Web サイトにアクセスする

以前は asp.net core で Web サイトを作成し、IIS で正常に動作していました。

今、Nginx を使用して Centos8 に変更します。

このプロジェクトはURLによってcentos8で正常に動作しますhttp://0.0.0.0:5001

リバースプロキシサーバーにはNginxを使用しています。しかし、ローカルコンピューターからWebサイトにアクセスすると502 Bad Gatewayエラーが報告されます。

ログは次のとおりですerror.log:

2020/01/24 08:47:47 [notice] 1#1: using the "epoll" event method
2020/01/24 08:47:47 [notice] 1#1: nginx/1.17.7
2020/01/24 08:47:47 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
2020/01/24 08:47:47 [notice] 1#1: OS: Linux 4.18.0-147.3.1.el8_1.x86_64
2020/01/24 08:47:47 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2020/01/24 08:47:47 [notice] 1#1: start worker processes
2020/01/24 08:47:47 [notice] 1#1: start worker process 6
2020/01/24 08:47:49 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 113.109.81.57, server: www.sealribbon.com, request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:5001/", host: "www.sealribbon.com"
2020/01/24 08:47:50 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 113.109.81.57, server: www.sample.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://0.0.0.0:5001/favicon.ico", host: "www.sealribbon.com", referrer: "https://www.sample.com/"
2020/01/24 08:47:59 [info] 6#6: *1 client 113.109.81.57 closed keepalive connection
2020/01/24 08:47:59 [info] 6#6: *2 client closed connection while waiting for request, client: 113.109.81.57, server: 0.0.0.0:443

そしてこれが私のnginx.conf:

user root;
worker_processes  1;
error_log  /var/log/nginx/error.log  info;
worker_rlimit_nofile 1024;
events {
    worker_connections  1024;
}
http {
    proxy_http_version 1.1;
    proxy_connect_timeout 600;
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    client_max_body_size 50m;
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    keepalive_timeout  65;
    gzip on;
    sendfile on;
    gzip_buffers 16 8k;
    gzip_comp_level 2;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon image/jpeg image/gif image/png;
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }    
upstream backend {
    server 0.0.0.0;
}
    server {
        listen       80;
        server_name  www.sample.com *.sample.com;
        return       301 https://$server_name$request_uri;
    }
    server {
        listen                    443 ssl;
        server_name               www.sample.com *.sample.com;
        ssl_certificate           /etc/nginx/cert/3390293_sample.com.pem;
        ssl_certificate_key       /etc/nginx/cert/3390293_sample.com.key;
        ssl_ciphers               ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols             TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_session_timeout       5m;
        location / {
            proxy_pass http://0.0.0.0:5001;
            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_redirect off;
            index index.html;
        }
    }
}

Google で検索したところ、同様の問題がたくさんあるようです。

誰かが私にsetsebool -P httpd_can_network_connect 1

試してみましたが、問題は依然として残っています。

私は初心者です。どうすれば解決できるか教えてください。ありがとうございます。

答え1

0.0.0.0有効な IP アドレスではありません。バックエンド サーバーの正しいアドレスを入力してください。

答え2

@Gerald Schneider さんの助力に感謝します。

次の 2 つのコードを変更します: アップストリーム バックエンド { server 172.18.92.125:5001; }

プロキシパスhttp://172.18.92.125:5001;

そしてついに、それは機能しました。

関連情報