简体   繁体   中英

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.

I build an application which is using 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. Here is my nginx conf file, which is a copy of the one I am using with Meteor and 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. On the other hand, when using subdomain.example.com:8100, React starts as expected.

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. In my error_log, I got that trace :

*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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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