簡體   English   中英

使用反向代理 Nginx 服務反應創建應用程序和 Nodejs 應用程序

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

試圖讓兩個應用程序一個反應創建另一個 Nodejs 在 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;

    #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 應用程序正在根目錄下提供,但 nodejs 應用程序文件未正確提供:

在此處輸入圖像描述

// 請嘗試使用此配置。

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:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM