Nginx反向代理不适用于域名

时间:2019-07-19 17:09:52

标签: node.js nginx reverse-proxy nginx-reverse-proxy centos-web-panel

我已经尝试了所有解决方案,但没有成功。我想将Nginx用作“ Node.js”应用反向代理。通过当前的配置,我能够通过服务器IP连接到服务器,但使用域名无法运行。我的配置详细信息pastebin.com/gMqpmDwj

http://Ipaddress:3000有效,但http://example.com无效。

这是我的Nginx代理的配置,存储在/etc/Nginx/conf.d/domain.conf中。

server {
    listen 80;
    server_name domain_name;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://ipaddress:3000;
    }
}

但是当我尝试访问它时,它在ip:port上可以正常工作,但是在domain:port上或没有port时却不能正常运行

2 个答案:

答案 0 :(得分:2)

尝试此配置:

/etc/nginx/nginx.conf

user                nobody;
worker_processes    1;
pid                 /var/run/nginx.pid;

events {
    worker_connections  1024;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    types_hash_max_size 2048;
    client_max_body_size 8M;
    server_tokens off;

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

    access_log off;
    error_log /var/log/nginx/error.log crit;

    gzip on;
    gzip_min_length 100;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/cloudflare.inc;
    include /etc/nginx/conf.d/*.conf;
}

/etc/nginx/conf.d/domain.conf

upstream nodejs_app {
    server <ipaddress>:3000;
    keepalive 8;
}

server {
    listen 80;
    listen [::]:80;
    server_name <domain_name>;

    location / {
        # websocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://nodejs_app/;
        proxy_redirect off;
    }
}

答案 1 :(得分:0)

点击此链接后我解决了我的问题。我有多个活动的配置文件,这引起了问题。 How to Configure Nginx Reverse Proxy for Nodejs on Centos