簡體   English   中英

反應不是從Nginx反向代理后面的node.js開始

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

這是我第一次嘗試在生產環境中設置一個Node.js服務器(除了一個Meteor服務器),並且遇到一個我無法自行解決的問題。

我構建了一個使用React的應用程序。 這是服務器代碼:

// 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')
})

我根據https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04進行了所有設置,即管理使用pm2並將Nginx配置為反向代理的服務器。 這是我的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;
    }
}

一方面,當我點擊subdomain.example.com時,我的index.html頁面沒有使用React。 另一方面,當使用subdomain.example.com:8100時,React會按預期啟動。

我該如何解決?

我找到了解決方案,所以在這里!

它來自nginx,需要訪問一些不允許的文件。 在我的error_log中,得到了該跟蹤:

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

使用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