【问题标题】:Nginx, Express.js and Node on port 80端口 80 上的 Nginx、Express.js 和 Node
【发布时间】:2016-01-29 22:59:33
【问题描述】:

我在端口 8000 上运行我的 Express.js 应用程序,我想在我的开发环境中使用 localhost 而不是 localhost:8000。

我在 Mac OSX El Capitan 上并使用 nginx 1.8.0

我收到 502 Bad GatewayERR_CONNECTION_TIMED_OUT 错误。

我通过sudo pkill nginx 停止 nginx 并从sudo nginx 开始。我也用pm2,通过sudo pm2 start bin/www --watch启动,这里没有问题。我可以在 localhost:8000 访问我的应用程序

我通过自制软件安装了 nginx。

/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

这是我在 /usr/local/etc/nginx 中的 nginx.conf

user  myusernameishere staff;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

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

启用站点/default.conf

server{
  listen 80;
  server_name 127.0.0.1 localhost;
  location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8000;
  }
}

/usr/local/etc/nginx/nginx.conf.default

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

【问题讨论】:

  • 我与 server_name localhost 发生冲突,将其保留为 127.0.0.1 并且没有问题。
  • 我在你的 nginx 配置文件中看到了 UpgradeConnection 标头。你尝试使用 websockets 吗?
  • 我确实使用 websockets (socket.io),但这不是这里的主要问题。原来 MAMP 的 Apache 也监听 80 端口。我把它卸载了,问题解决了。几分钟后,我再次安装了 MAMP,启动了 Apache 但它没有再次发生,这真的很奇怪。我现在不知道该说什么。

标签: node.js express nginx


【解决方案1】:

Upgrade 是 HTTP v1.1 的标头,使用时可能需要包含 proxy_http_version 1.1;

【讨论】:

    【解决方案2】:

    试试这个sites_enabled/default.conf

    upstream backend {
      server localhost:8000;
    }
    
    server{
      listen 80;
      server_name 127.0.0.1 localhost;
      location / {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://backend;
      }
    }
    

    【讨论】:

    • 它给出了nginx: [emerg] invalid parameter "server_name" in /usr/local/etc/nginx/sites-enabled/default.conf:3 错误。即使我改正了,我仍然无法获取 localhost。
    • @salep - 在上面的 sites_enabled/default.conf 的后端部分中存在错误(已更正)。我在 El Capitan w/nginx 1.8.0 安装的视图 brew 上尝试了这个,它看起来也像你的设置。 nginx 部分正在工作。如果您仍然收到 502,则可能是端口 8000 上的应用程序存在问题。
    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多