简体   繁体   English

反应不是从Nginx反向代理后面的node.js开始

[英]React not starting with node.js behind Nginx Reverse Proxy

It's the first time I try to set up a Node.js server in production (apart from one Meteor server), and I am encountering a problem I am not able to solve on my own. 这是我第一次尝试在生产环境中设置一个Node.js服务器(除了一个Meteor服务器),并且遇到一个我无法自行解决的问题。

I build an application which is using React. 我构建了一个使用React的应用程序。 Here is the server code: 这是服务器代码:

// Import part
var react = require('react');
var express = require('express');
var hogan = require('hogan-express');

// Express
const app = express()
app.engine('html', hogan)
app.set('views', __dirname + '/views')
app.use('/', express.static(__dirname + '/public'))
app.set('port', (process.env.PORT || 8100))

app.get('*',(req, res) => {
    res.status(200).render('index.html')
})

I set up everything according to https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04 ie managing the server using pm2 and configuring Nginx as a reverse proxy. 我根据https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04进行了所有设置,即管理使用pm2并将Nginx配置为反向代理的服务器。 Here is my nginx conf file, which is a copy of the one I am using with Meteor and React : 这是我的nginx conf文件,是我与Meteor和React一起使用的文件的副本:

upstream node {
    server localhost:8100;
}

server {
    listen 80;
    listen [::]:80;

    server_name subdomain.example.com;
    server_tokens off;

    access_log /var/log/nginx/node_access.log;
    error_log /var/log/nginx/node_error.log;

    location / {
        client_max_body_size 0;
        gzip off;
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header    Host            $http_host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-Proto   $scheme;
        proxy_pass http://node;
    }
}

On one hand, when I hit subdomain.example.com, I got my index.html page served without React. 一方面,当我点击subdomain.example.com时,我的index.html页面没有使用React。 On the other hand, when using subdomain.example.com:8100, React starts as expected. 另一方面,当使用subdomain.example.com:8100时,React会按预期启动。

How could I solve this ? 我该如何解决?

I found the solution, so here it is ! 我找到了解决方案,所以在这里!

It was coming from nginx which needed to access some files it was not allowed to. 它来自nginx,需要访问一些不允许的文件。 In my error_log, I got that trace : 在我的error_log中,得到了该跟踪:

*1 open() "/var/lib/nginx/proxy/3/00/0000000003" failed (13: Permission denied) while reading upstream

Using Google lead me to this page https://github.com/dockerfile/nginx/issues/4 , which gave me the solution : 使用Google引导我到此页面https://github.com/dockerfile/nginx/issues/4 ,这给了我解决方案:

chown -R www-data:www-data /var/lib/nginx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM