如何使用gunicorn和nginx在同一个Ubuntu 18.04服务器上部署Django和React项目



我有一个Django项目,我已经使用本教程通过gunicorn和nginx成功地部署在我的Ubuntu 18.04服务器上。

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04

该项目使用Django Rest Framework,我可以通过web浏览器访问它的端点。然而,我也想在同一台服务器上部署一个单独的react项目,这样它就可以向Django应用程序发送http请求,并显示从RESTneneneba API接收的数据。我该怎么做?

这是我当前的gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/my_project/coffeebrewer
ExecStart=/home/ubuntu/my_project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/ubuntu/my_project/coffeebrewer/coffeebrewer.sock coffeebrewer.wsgi:application
[Install]
WantedBy=multi-user.target

以下是我当前的nginx配置

server {
listen 80;
listen [::]:80;
server_name my_ipv6_address;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root root /home/ubuntu/my_project/coffeebrewer;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

我建议您通过安装whitenoise django package并在settings.py文件中添加此行SECURE_CROSS_ORIGIN_OPENER_POLICY = None,将其作为生产模式先在本地试用

从那里,你可以导航向前

最新更新