"无法访问此网站"使用Nginx作为Express Nodejs的反向代理

时间:2018-04-05 02:46:40

标签: node.js http express nginx

我无法从外部访问节点服务器。在内部,我可以很好地访问它,但我不能这样做。

这是我的nginx配置。我只想使用我的外部IP访问我的网站(例如,133.21.29.21)

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        listen 2500;

        location / {
          proxy_pass http://127.0.0.1:3005;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
}

服务器代码

    app.server.listen(3003, "0.0.0.0", () => {
            console.log(app.server.address());
    });

此刻我在3003端口上运行。我已经尝试过服务器nginx配置并更改我的服务器代码(更改端口,省略" 0.0.0.0",使用" 127.0.0.1")但我没有运气

我一直试图通过访问my-external-ip:2500来访问我的服务器,但我也试过通过其他端口访问。

我已经禁用了ufw防火墙,但仍然没有运气。本地冰壶工作正常。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我认为您缺少代理重定向

看看下面的示例NGINX配置文件,位置/ {}指向端口9080上的节点服务器,它的工作方式是导航到https:// ... .com

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/thedomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/thedomain.com/privkey.pem;

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

    server_name _;

    root            /var/www/html;

    index           index.html;

    gzip on;
    gzip_proxied any;
    gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json;

    location /.well-known/ {
        try_files   $uri $uri/ =404;
    }

    location /jenkins {
        include /etc/nginx/proxy_params;
        proxy_pass          http://localhost:8080;
        proxy_read_timeout  90s;
        proxy_redirect      http://localhost:8080 https://www.thedomain.com/jenkins;
    }

    location /wss/pforex {
        include /etc/nginx/proxy_params;
        proxy_pass          http://localhost:9190;

        proxy_http_version  1.1;
        proxy_read_timeout  90s;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection $connection_upgrade;
        proxy_redirect      http://localhost:9190 https://www.thedomain.com/wss/pforex;
    }

    location / {
        include /etc/nginx/proxy_params;
        proxy_pass          http://localhost:9080;
        proxy_read_timeout  90s;
        proxy_redirect      http://localhost:9080 https://www.thedomain.com;
    }
}

答案 1 :(得分:0)

Nginx作为前端服务器,在这种情况下代理请求到node.js服务器。因此,您需要为节点设置Nginx配置文件。

在/ etc / nginx / sites-available /:

创建文件yourdomain.com
# the IP(s) on which your node server is running. I chose port 3003.
upstream app_yourdomain {
    server 127.0.0.1:3003; # can use localhost as well
    keepalive 8;
}

# the Nginx server instance
server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;
    access_log /var/log/nginx/yourdomain.com.log;

    # pass the request to the node.js server with the correct headers
    # and much more can be added, see Nginx config options
    location / {
      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://app_yourdomain/;
      proxy_redirect off;
    }
 }

此外你甚至可以省略给ip listen方法,默认情况下会使用localhost

const app = express();

 app.listen(3003, () => {
        console.log(app.server.address());
 });

如果您使用IP直接访问服务器,则需要更改

server_name yourdomain.com www.yourdomain.com;

server_name _;