简体   繁体   中英

Nginx as reverse proxy for Node.js app on CentOS 7

I have a node app running on server port 5000 and is accessible from my browser. Apache is not installed. I followed a guide on installing and configuring Nginx and I was able to successfully open the default HTML page served for centos Nginx installations which means that port 80 is open (no SSL yet). Then I edited the location settings in /etc/nginx/nginx.conf by adding:

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://localhost:5000;
    }

Following the successfully restart with sudo systemctl restart nginx , I am now served a default error page that came with nginx installation instead of my app that runs on server_ip:5000. How do I properly debug it and find out why reverse proxy is not working?

My full nginx.conf (I removed the commented SSL settigns only):

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

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://localhost:5000;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

The error page I am served is: /usr/share/nginx/html/50x.html . 在此处输入图片说明

Please update your server block as provided below:

  server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:5000;
        proxy_redirect off;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

Also, make sure your application is listening on localhost:5000 . Sometimes, the service gets bind only to interface IPs but not with localhost.

If you still get issue, please check your nginx error logs and provide the error message from logs.

Along with the nginx config file, it's an issue of selinux module httpd_can_network_connect which is disabled by default. You can enable it by the below command:

setsebool -P httpd_can_network_connect on

More details can be found at https://wiki.centos.org/TipsAndTricks/SelinuxBooleans

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM