NGINX - 在一个端口上提供 create-react-app 应用程序,在另一个端口上提供我的 api

NGINX - 在一个端口上提供 create-react-app 应用程序,在另一个端口上提供我的 api

在我的应用程序中,我使用 create-react-app,并通过在节点 pm2 进程中使用“serve -s build”在端口 5000 上为我的站点前端提供服务。我可以通过对我的 nginx 使用此配置来实现这一点。

server {
listen 443 ssl;
server_name random.com www.random.net;
ssl_certificate /etc/letsencrypt/live/random.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/random.net/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 / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:5000";
    }

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

server {
    listen 80;
    server_name random.net www.random.net;
    return 301 https://$host$request_uri;
}

但是,当我将此部分添加到我的配置中时,它会破坏我的标准位置,并且 api 端点不起作用。这是在另一个 pm2 实例中运行的另一个为我的 api 提供服务的进程,我希望我的 api 下的这些路由通过 express 提供服务以返回 json 文件。

location /api {
    proxy_pass         "http://127.0.0.1:9000";
}

我得到的最终配置如下,但它不起作用。

server {
listen 443 ssl;
server_name random.com www.random.net;
ssl_certificate /etc/letsencrypt/live/random.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/random.net/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 / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://127.0.0.1:5000";
    }
    location /api {
        proxy_pass         "http://127.0.0.1:9000";
    }

    location ~ /.well-known {
        allow all;
    }
}
server {
    listen 80;
    server_name random.net www.random.net;
    return 301 https://$host$request_uri;
}

如果有人能告诉我我做错了什么以及如何解决它,那就太好了。谢谢!:))

答案1

来自评论

proxy_pass 中的引用有问题。它应该是这样的:

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

location /api {
    proxy_pass         http://127.0.0.1:9000;
}

相关内容