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

发布于 2025-01-11 07:38:47 字数 1342 浏览 0 评论 0原文

我有一个 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 请求并显示从 REST 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;
    }
}

I have a Django project that I have already successfully deployed on my Ubuntu 18.04 server via gunicorn and nginx using this tutorial.

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

The project uses Django Rest Framework and I'm able to access it's endpoints via a web browser. However, I would also like to deploy a separate react project on the same server, so that it can send http requests to the Django app and display data received from the REST API. How can I go about doing this?

Here is my current 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

And here are my current nginx configurations

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;
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2025-01-18 07:38:47

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

从那里,您可以向前导航

I recommend that you try it locally first as a production mode by installing whitenoise django package and in your settings.py file add this line SECURE_CROSS_ORIGIN_OPENER_POLICY = None

From there, you can navigate forward

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文