Node + Nginx反向代理永远不会工作

时间:2021-07-24 03:34:23

I've spent so much time trying to get this work that it's cutting into actual development time. I'm trying to set up two domains on my site example.com and example2.com but navigating to the domain in browser results in a 502 at best.

我花了很多时间试图完成这项工作,它正在削减实际的开发时间。我正在尝试在我的网站example.com和example2.com上设置两个域,但在浏览器中导航到域时最多只能获得502。

The etc/nginx/nginx.conf that I got from a friend:

我从朋友那里得到的etc / nginx / nginx.conf:

user root;
worker_processes 4;


events {
        worker_connections 768;
}

http {

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

        server_names_hash_bucket_size 64;

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

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


        gzip on;
        gzip_disable "msie6";

        include /etc/nginx/sites-enabled/*;
}

The etc/nginx/sites-available/sites that I also got from a friend:

我从朋友那里得到的etc / nginx / sites-available / sites:

server {
    server_name example.com;
    # proxy pass to nodejs
    location / {
        proxy_pass    http://127.0.0.1:9001/;
    }
}
server {
    server_name example2.com;
    # proxy pass to nodejs
    location / {
        proxy_pass    http://127.0.0.1:8000/;
    }
}

I've been working in circles and have tried so many different configurations that all fail, this specific one results in a Gateway error 502 for both sites.

我一直在圈子里工作并尝试了很多不同的配置,这些配置都失败了,这个特定的配置导致两个站点的网关错误502。

I've never been able to get this to work and I have no idea why, A second set of eyes would be very helpful.

我从来没有能够让这个工作,我不知道为什么,第二组眼睛会非常有帮助。

1 个解决方案

#1


1  

There is nothing that looks wrong in your nginx configuration. If the Node server to which nginx is trying to forward a connection is not actually listening on the port you specify in your configuration you will get a 502 status code.

您的nginx配置中没有任何错误。如果nginx尝试转发连接的节点服务器实际上没有侦听您在配置中指定的端口,那么您将获得502状态代码。

To verify whether it is listening correctly, you can use any tool that can connect to a port to perform an HTTP GET. So, for instance:

要验证它是否正确侦听,您可以使用任何可以连接到端口的工具来执行HTTP GET。所以,例如:

$ curl http://127.0.0.1:9001/

Or wget could also be used for this. If the connection is refused chances are that your node server is not running at all or configured to listen on a different port.

或者wget也可以用于此。如果连接被拒绝,则可能是您的节点服务器根本没有运行或配置为侦听其他端口。

#1


1  

There is nothing that looks wrong in your nginx configuration. If the Node server to which nginx is trying to forward a connection is not actually listening on the port you specify in your configuration you will get a 502 status code.

您的nginx配置中没有任何错误。如果nginx尝试转发连接的节点服务器实际上没有侦听您在配置中指定的端口,那么您将获得502状态代码。

To verify whether it is listening correctly, you can use any tool that can connect to a port to perform an HTTP GET. So, for instance:

要验证它是否正确侦听,您可以使用任何可以连接到端口的工具来执行HTTP GET。所以,例如:

$ curl http://127.0.0.1:9001/

Or wget could also be used for this. If the connection is refused chances are that your node server is not running at all or configured to listen on a different port.

或者wget也可以用于此。如果连接被拒绝,则可能是您的节点服务器根本没有运行或配置为侦听其他端口。