Nginx没有正确代理nodejs

时间:2017-04-12 08:15:40

标签: node.js nginx

我目前在我的nginx服务器上设置了ssl。我还有一个在端口80上运行的节点应用程序。我正在尝试将所有请求反向代理到nginx的/api路由,但它没有正确执行。

当我执行curl localhost:8080时,它会正确显示Welcome to hooq

然而,当我对直接的webserver ip地址hooq.digitalfolks.sg/api执行curl命令时,它会抛出404错误,我在路由未找到时在节点中设置了错误。

这是我目前的nginx conf

server {
        listen 443 ssl;

        server_name hooq.digitalfolks.sg;

        ssl_certificate /etc/letsencrypt/live/hooq.digitalfolks.sg/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/hooq.digitalfolks.sg/privkey.pem;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_dhparam /etc/ssl/certs/dhparam.pem;
        ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
        ssl_session_timeout 1d;
        ssl_session_cache shared:SSL:50m;
        ssl_stapling on;
        ssl_stapling_verify on;
        add_header Strict-Transport-Security max-age=15768000;

        location / {

        }

        location ~ /api {
                proxy_pass http://127.0.0.1:8080;
        }

        location ~ /.well-known {
                allow all;
        }

}


server {
    listen 80;
    server_name hooq.digitalfolks.sg;
    return 301 https://$host$request_uri;
}

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

如果您的Node应用程序不希望路由中的/api,请尝试更改此内容:

    location ~ /api {
            proxy_pass http://127.0.0.1:8080;
    }

到此:

    location ~ /api {
            rewrite ^/api/(.*)$ /$1 break;
            proxy_pass http://127.0.0.1:8080;
    }

您没有包含Node应用程序的代码,因此无法确定是否属于此类情况,但这是反向代理配置的常见问题 - 请参阅此答案以获取更多详细信息: