gpt4 book ai didi

node.js - 将 Nginx 设置为 Nodejs 和 Django 的代理

转载 作者:搜寻专家 更新时间:2023-10-31 23:26:15 25 4
gpt4 key购买 nike

我的机器本地有一个 Web 项目。我在前端使用 js 框架,在后端使用 django,它提供 API 来与前端应用程序通信。 Node 应用程序在端口 3000 上运行,django 在端口 8000 上运行。两者在我的本地机器上都运行良好。现在我想将它部署到生产环境中,并想为反向代理设置 Nginx。在过去,我只将 nginx 设置为 django 网站的反向代理,如下所示:

server {
server_name someproject.com www.someproject.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/someproject/static_cdn/;
}

location / {
include proxy_params;
proxy_pass http://unix:/home/someproject/someproject.sock;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/someproject.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/someproject.com/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.someproject.com) {
return 301 https://$host$request_uri;
} # managed by Certbot


if ($host = someproject.com) {
return 301 https://$host$request_uri;
} # managed by Certbot


listen 80;
server_name someproject.com www.someproject.com;
return 404; # managed by Certbot
}

但是,我如何使用 ssl 作为 Node 服务器的反向代理配置和保护 Nginx 作为在端口 3000 上运行的前端,以及 Django 作为其后端在端口 8000 上运行。我希望我是清楚的,并且如果没有请问。谢谢。

最佳答案

有很多方法可以构建它,具体取决于您希望 Node 应用程序如何与 Django 应用程序通信。最简单的方法是将您的 nginx 配置分为 两个 单独的 server_name 配置。例如,www.someproject.com => node 和 api.someproject.com for django:

server {
server_name www.someproject.com;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/someproject/static_cdn/;
}

location / {
include proxy_params;
proxy_pass http://unix:/home/someproject.node/someproject.sock;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.someproject.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.someproject.com/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 {
server_name api.someproject.com;

location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/home/someproject.django/someproject.sock;
}

listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.someproject.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.someproject.com/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 {
# redirect someproject.com => www.someproject.com
listen 80;
listen 443 ssl;
server_name someproject.com;
location / {
return 301 https://www.someproject.com$request_uri;
}
}

server {
listen 80;
server_name www.someproject.com api.someproject.com;
# redirect http => https
location / {
return 301 https://$host$request_uri;
}
}

关于node.js - 将 Nginx 设置为 Nodejs 和 Django 的代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49118070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com