简体   繁体   English

使用反向代理 Nginx 服务反应创建应用程序和 Nodejs 应用程序

[英]serve react create app and Nodejs app with reverse proxy Nginx

Trying to have both apps one react create the other Nodejs run behind Nginx proxy.试图让两个应用程序一个反应创建另一个 Nodejs 在 Nginx 代理后面运行。 The followings are my configs:以下是我的配置:

server {
    listen       443 ssl;
    server_name  site.com;
    ssl_certificate     /etc/site.com.pem;
    ssl_certificate_key /etc/site.com.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;



    location /nodejs {
        root  /usr/share/nodejs;
        proxy_pass http://my.url.com:3009;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-for $remote_addr;
    }

    location / {
        root  /usr/share/react-create;
        proxy_pass http://my.url.com:3011;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-for $remote_addr;
    }

React app is being served at root but nodejs app files are not being served correctly: React 应用程序正在根目录下提供,但 nodejs 应用程序文件未正确提供:

在此处输入图像描述

// Please try with this configuration. // 请尝试使用此配置。

upstream nodejs {
  server http://my.url.com:3009; 
}

upstream reactjs {
  server http://my.url.com:3007; 
} 

server {
listen       443 ssl;
server_name  site.com;
ssl_certificate     /etc/site.com.pem;
ssl_certificate_key /etc/site.com.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers         HIGH:!aNULL:!MD5;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location /node {
    root  /usr/share/nodejs;
    proxy_pass http://nodejs/api;
    proxy_set_header Host $host;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-for $remote_addr;
}

location /react {
    root  /usr/share/react-create;
    proxy_pass http://reactjs;
    proxy_set_header Host $host;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-for $remote_addr;
}

First let Nginx handle serving your react static files form their build file, and reorder the location matching for Nginx and let the nodejs or the api server for later catch for Nginx: First let Nginx handle serving your react static files form their build file, and reorder the location matching for Nginx and let the nodejs or the api server for later catch for Nginx:

server {
   listen       443 ssl;
   server_name  site.com;
   ssl_certificate     /etc/site.com.pem;
   ssl_certificate_key /etc/site.com.key;
   ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
   ssl_ciphers         HIGH:!aNULL:!MD5;

   root /path/to/project-base/build-live/;
   index  index.html;

   location / {
     try_files $uri /index.html =404;
   }

   location /api {
      proxy_pass http://myapistream;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 proxy_pass 在 NGINX 中提供 NodeJS HTTPS 应用程序 - Serve a NodeJS HTTPS app, in NGINX via proxy_pass 可在 nginx 反向代理后面的端口 3000 上访问 nodejs 应用程序 - nodejs app accessible on port 3000 behind nginx reverse proxy 静态内容Node.js应用反向代理的NgInx禁止错误 - NgInx forbidden error for static content nodejs app reverse proxy 运行 nginx 来提供文件并充当同一域上 Node 应用程序的反向代理 - Running nginx to serve files and act as a reverse proxy for Node app on same domain 如何使用多个 Express 应用程序 + NGINX 作为反向代理服务器提供 static 文件(CSS,...) - How to serve static files (CSS, …) with multiples Express app + NGINX as reverse proxy server NodeJS或NGINX for Reverse Proxy - NodeJS or NGINX for Reverse Proxy 是否像php一样从nginx服务nodejs应用程序? - Serve nodejs app from nginx like with php? 在运行Nginx的反向代理的apache上运行的服务器上安装Nodejs应用 - Install Nodejs app on server running on apache which uses reverse proxy with nginx HTTP 404 与 Nodejs 应用程序在 ubuntu 18.04 AWS 服务器运行 Nginx 反向代理 - HTTP 404 with Nodejs app on ubuntu 18.04 AWS server running Nginx reverse proxy nodejs 反向代理代替 nginx 作为反向代理 - nodejs reverse proxy instead of nginx as reverse proxy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM