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

时间:2019-10-23 21:53:45

标签: node.js reactjs nginx reverse-proxy

试图让两个应用程序一个反应创建另一个在Nginx代理后面运行的Nodejs。以下是我的配置:

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;
    }

在根目录下提供了反应应用程序,但没有正确提供nodejs应用程序文件:

enter image description here

2 个答案:

答案 0 :(得分:2)

//请尝试使用此配置。

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;
}

答案 1 :(得分:1)

  

首先让Nginx处理从其生成文件中提供您的React静态文件,然后重新排列与Nginx匹配的位置,并让nodejsapi服务器供以后捕获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;
    }
}