在 Digitalocean 上使用 nginx 部署 Express 服务器和 React JS

发布于 2025-01-09 17:22:02 字数 3659 浏览 0 评论 0原文

这是我第一次在 DO 上使用 Nginx 部署 Expressjs 和 Reactjs。我能够将前端设置为从互联网访问,但我在后端遇到问题。我已经看过多个问题、视频、指南,但仍然挣扎了一个星期。希望能得到一些见解!非常感谢。

测试站点链接:www.simpletodo.xyz

我主要遵循的指南链接:https://gist.github.com/sjosephrw/5bc7efbf4c332070165c61dba253288d

App.js(相关位)

<button
    onClick={(e) => {
        e.preventDefault();

        fetch('https://www.simpletodo.xyz/backend/test')
            .then((res) => res.json())
            .then((res) => setTest(res))
            .catch((err) => console.log(err));
    }}>
    Test Backend
</button>
{test && <div>{test.text}</div>}

server.js (node/express)

const express = require('express');
const app = express();
const PORT = 4000;
const cors = require('cors');

app.use(cors());

app.get('/test', (req, res) => {
    console.log('is it gonna work??')
    res.json({ text: 'Broooo it works!!!!' });
});

app.listen(PORT, () => {
    console.log(`Server is listening on port ${PORT}`);
});

< strong>/etc/nginx/sites-available/simpletodo.xyz.conf

server{
        #Path to React front end
        root /var/www/html/simpletodo.xyz;

        #Main html file
        index index.html;

        #Server name
        server_name simpletodo.xyz www.simpletodo.xyz;

        #Store access log files
        access_log /var/log/nginx/simpletodo.xyz.access.log;

        #Store error log
        error_log /var/log/nginx/simpletodo.xyz.error.log;

        location / {
                proxy_pass http://localhost:3000;
                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;
        }

        location /backend/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://127.0.0.1:4000;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;
        }
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/simpletodo.xyz/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/simpletodo.xyz/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server{
    if ($host = www.simpletodo.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = simpletodo.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
        listen 80;
        listen [::]:80;
        server_name simpletodo.xyz www.simpletodo.xyz;
    return 404; # managed by Certbot
}

前端和后端都在使用pm2

Netstat 输出显示后端正在端口 4000 上运行 netstat

/var/log/nginx/log
log

it's my first time deploying Expressjs and Reactjs using Nginx on DO. I was able to setup the frontend to be accessed from internet but I'm having trouble with the backend. I've looked at multiple SO questions, videos, guides and still struggling for a week now. Hope to get some insights! Much appreciated.

Link to test site: www.simpletodo.xyz

Link to the guide I mainly follow: https://gist.github.com/sjosephrw/5bc7efbf4c332070165c61dba253288d

App.js (relevant bit)

<button
    onClick={(e) => {
        e.preventDefault();

        fetch('https://www.simpletodo.xyz/backend/test')
            .then((res) => res.json())
            .then((res) => setTest(res))
            .catch((err) => console.log(err));
    }}>
    Test Backend
</button>
{test && <div>{test.text}</div>}

server.js (node/express)

const express = require('express');
const app = express();
const PORT = 4000;
const cors = require('cors');

app.use(cors());

app.get('/test', (req, res) => {
    console.log('is it gonna work??')
    res.json({ text: 'Broooo it works!!!!' });
});

app.listen(PORT, () => {
    console.log(`Server is listening on port ${PORT}`);
});

/etc/nginx/sites-available/simpletodo.xyz.conf

server{
        #Path to React front end
        root /var/www/html/simpletodo.xyz;

        #Main html file
        index index.html;

        #Server name
        server_name simpletodo.xyz www.simpletodo.xyz;

        #Store access log files
        access_log /var/log/nginx/simpletodo.xyz.access.log;

        #Store error log
        error_log /var/log/nginx/simpletodo.xyz.error.log;

        location / {
                proxy_pass http://localhost:3000;
                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;
        }

        location /backend/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://127.0.0.1:4000;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;
        }
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/simpletodo.xyz/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/simpletodo.xyz/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server{
    if ($host = www.simpletodo.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = simpletodo.xyz) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
        listen 80;
        listen [::]:80;
        server_name simpletodo.xyz www.simpletodo.xyz;
    return 404; # managed by Certbot
}

Both frontend and backend are running using pm2

Netstat output shows backend is running on port 4000
netstat

/var/log/nginx/log
log

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文