简体   繁体   English

部署到 nginx Web 服务器时出现 502 bad gateway 错误

[英]502 bad gateway error when deploying to nginx web server

I built a react app using the create-react-app and npm run build commands and connected it to node with server.js file in the directory created by create-react-app .我建了一个使用应用程序的反应create-react-appnpm run build命令,并将其与连接到节点server.js由创建的目录文件create-react-app

When running the command node server locally it works perfectly fine however when I pushed the changes to my nginx server I started to get a 502 bad gateway status.在本地运行命令node server ,它工作得很好,但是当我将更改推送到我的 nginx 服务器时,我开始收到 502 错误的网关状态。 Why is this happening?为什么会这样? Node is running when I get this error.当我收到此错误时,节点正在运行。

Here is the server.js code这是server.js代码

onst express = require('express');
const path = require('path');

const app = express();


app.use('/js', express.static(path.join(__dirname, 'src/')));
app.use('/css', express.static(path.join(__dirname, 'src/')));

app.use(express.static(path.join(__dirname , '/public/build')));


// Handles any requests that don't match the ones above
app.get('/', (req,res) =>{
    res.sendFile(path.join(__dirname , "/public/build/index.html"));

});

const port = process.env.PORT || 5000;
app.listen(port);

console.log('App is listening on port ' + port);

the error log错误日志

[error] 7422#7422: *4477 connect() failed (111: Connection refused) while connecting to upstream, client: 162.84.158.175, server: anthonyjimenez.me, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:3000/", host: "anthonyjimenez.me"

and the config file和配置文件

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

go to /nginx/sites-enabled/default and add the following blocks.转到 /nginx/sites-enabled/default 并添加以下块。 You will have to tell Nginx and forward this request this way您必须告诉 Nginx 并以这种方式转发此请求

server {

listen 443 ssl ;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; //ssl
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; //ssl

index index.html index.htm index.nginx-debian.html;
server_name example.com;

location / {
proxy_pass http://localhost:3001; //Your port goes here
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}


}

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

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