Nginx代理

在项目中配置Nginx

在项目目录下添加一个config文件夹,后面的配置文件都统一放到该文件夹下(配置文件存放路径不统一会导致输入提示符时运行不成功)

config文件下新建mysite_nginx.conf文件,内容如下(将路径替换成你自己的项目路径或者对应文件):

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # unix:///home/breavo/PyWorkSpace/mysite_code_shuffle/config/eshop.sock
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name xx.xx.xxx.xxx; # substitute your machine's IP address or FQDN
    charset     utf-8;

# max upload size
    client_max_body_size 75M;   # adjust to taste

# Django media
    location /media  {
        # /home/breavo/PyWorkSpace/mysite_code_shuffle/meida;
        alias /path/to/your/mysite/media; # your Django project's media files - amend as required
    }

location /static {
        # /home/breavo/PyWorkSpace/mysite_code_shuffle/static
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

# Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/breavo/PyWorkSpace/mysite_code_shuffle/config/uwsgi_params; # the uwsgi_params file you installed
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
在/etc/nginx/sites-enabled中创建一个链接,这样Nginx能够发现并启用我们的配置,运行

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
1
部署静态文件

在运行Nginx之前,需要将django项目中的所有静态文件放入static文件夹,在mysite/settings.py中添加

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
1
然后运行

python manage.py collectstatic
1
启动(重启)Nginx

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx restart
1
2
简单测试

往服务器项目的media文件夹里丢一个图片(这里以test.png为例)

访问

xx.xx.xxx.xxx:8000/media/test.png
1

图片正确显示~

至此,静态文件部署算是完成了

Uwsgi安装及配置

接下来我们需要使用Uwsgi来处理项目中的动态请求

基本配置

虚拟环境下运行

pip install uwsgi

在之前创建的config文件夹下新建一个hello.py用来做测试,写入以下内容

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2
1
2
3
4
5
接下来运行

uwsgi --http :8001 --wsgi-file test.py

打开浏览器查看

出现上图,说明Uwsgi已经初步运行成功

运行项目

上述步骤没有出问题的话,我们就可以使用Uwsgi启动项目了,项目根目录下运行

uwsgi --http :8001 --module mysite.wsgi

其中mysite.wsgi(mysite替换成自己的项目名称)会自动搜索项目中的wsgi.py文件

如果正常运行~恭喜

如果浏览器出现Internal Server Error

检查命令提示符是否输入正确
检查是否项目根目录下运行
其他情况,查看命令行输出或者log
至此Uwsgi基本配置算是完成了~~

Nginx + Uwsgi + django

Nginx访问hello.py

现在我们使用socket通信来通过Nginx访问hello.py,(确保Nginx已经启动)config文件夹下运行

uwsgi --socket :8001 --wsgi-file hello.py

好了,就这么现在访问浏览器

xx.xx.xxx.xxx:8000

成功访问~

其中8000是Nginx监听的端口(mysite_nginx.conf文件中配置),–socket :8001是用于socket通信的端口(两个端口都可以替换~)

使用Unix sockets替换端口

编辑mysite_nginx.conf文件,将原来内容修改成如下

server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
1
2
保存后,config文件夹下运行

uwsgi --socket mysite.sock --wsgi-file hello.py

依旧打开浏览器访问xx.xx.xxx.xxx:8000,屏幕出现hello world,说明一切正常

如果出现502 Bad Getway

查看你的Nginx error log(/var/log/nginx/error.log)

如果出现

connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
denied)
1
2
则说明是权限问题,运行

uwsgi --socket mysite.sock --wsgi-file hello.py --chmod-socket=666

或者

uwsgi --socket mysite.sock --wsgi-file hello.py --chmod-socket=664 (推荐,但是我用了没成功)

log如果打印了其他问题,请仔细检查配置~

使用Nginx + Uwsgi运行项目

uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
1
注意这里有个小坑,mysite_nginx.conf中我配置的mysite.sock在config文件夹下,而这条命令需要在项目根目录下运行,这里可以:

修改mysite_nginx.conf下sock文件路径到根目录下
使用配置文件启动(推荐)
编辑配置文件

config文件夹下新建mysite_uwsgi.ini,输入以下内容

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /path/to/your/project
# Django's wsgi file
module          = project.wsgi
# the virtualenv (full path)
home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

stats           = %(chdir)/config/uwsgi.status

pidfile         = %(chdir)/config/uwsgi.pid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
用自己的路径替换其中对应的配置~

保存后,运行(确保在mysite_uwsgi.ini目录下)

uwsgi --ini mysite_uwsgi.ini

打开浏览器访问xx.xx.xxx.xxx:8000

项目这时候应该完美运行了~~

大功告成~:)
--------------------- 
作者:泡泡茶壶 
来源:CSDN 
原文:https://blog.csdn.net/breavo_raw/article/details/82665978 
版权声明:本文为博主原创文章,转载请附上博文链接!

项目部署uwsgi +Nginx相关推荐

  1. Django项目部署 uwsgi+nginx 负载均衡

    部署示意图 uwsig 第一步:安装uwsig pip install uwsgi 第二步:配置uwsgi 手动在项目根目录下新建文件uwsgi.ini [uwsgi]# 使用nginx连接时使用 # ...

  2. linux 退出服务器_Vue实战091:Vue项目部署到nginx服务器

    项目开发完成之后我们就需要将项目上线运行供用户访问,这时候我们就需要将项目部署到服务器上.对于Vue这种前端Web项目我们一般都部署在linux系统上,Linux常见的Web应用服务器有Apache. ...

  3. vue项目部署到nginx

    vue项目部署到nginx 一.vue项目打包 修改config/index.js的assetsPublicPath为 ./(注意一定是build里面的,下面dev中也有这个配置) 这个路径的配置很重 ...

  4. vue-cli部署ngixs_Vue-cli项目部署到Nginx

    项目环境: 0. Nginx使用 以windows版为例,下载niginx压缩包并解压到任意目录,双击nginx.exe,在浏览器中访问http://localhost,如果出现Welcome to ...

  5. 将Vue项目部署在Nginx,解决前端路由、反向代理和静态资源问题

    将Vue项目部署在Nginx,解决前端路由.反向代理和静态资源问题 需求: 一台服务器,Linux 安装了Nginx 使用Vue脚手架编写的Vue项目 第一步:将Vue项目打包,然后将生成的dist文 ...

  6. nginx怎么部署php项目,如何php项目部署到nginx

    php项目部署到nginx的方法:首先安装"Nginx"和"php-fpm":然后将项目复制到"nginx/html"文件下:接着修改配置文 ...

  7. python-django 阿里云ECS服务器部署uwsgi+nginx过程解说

    需求:在本地已经完成了一个博客系统/数据可视化系统的开发,需要部署到系统上,展示个人主页/个人作品 编程语言:Python 系统框架:Django 数据库:MySQL 服务架构:uwsgi+nginx ...

  8. 多个前端项目部署在nginx中同一个server下

    多个前端项目部署在同一个域名下 在vue.config.js中设置 publicPath: '/web/' 在路由index.js中设置 base:'/web/' 在index.html中加入 修改N ...

  9. 将项目部署到nginx服务器上

    一. 介绍 服务器:本质是一台电脑,没有显示器,就是主机,24小时开机,为用户提供远程服务. 市面上的服务器:阿里云/华为云/腾讯云(配置) 主机->操作系统->window(.net)/ ...

最新文章

  1. 使用GitLab或者Github简单实用地将数据导入Colab的方法
  2. unity3d游戏开发第2版 pdf_学开发2个月,1个人做出个TapTap排行榜第4的游戏?
  3. c++ primer 习题13.39自己做的答案
  4. 「每天一道面试题」String和StringBuilder、StringBuffer的区别
  5. Method Tracking
  6. gwt-2.8.2下载_从GWT开发人员的角度概述Scala.js
  7. 步骤一:入门linux基础/01Linux简介和安装/003Linux系统的多面性
  8. Android:新建一个Activity(隐式/显式),并携带数据
  9. JAVA:泛型通配符T,E,K,V区别,T以及Class,Class的区别
  10. 谁在维护linux内核,故意向Linux内核中提及漏洞? Linux 内核维护者封杀明尼苏达大学...
  11. Unity贴图ASTC压缩格式
  12. 输入半径,求球的表面积和体积
  13. iOS TouchID和FaceID登录验证 简单使用
  14. 国外也有“天价流量账单” 看看都什么情况?
  15. 小米手机定价与《怪诞行为学》
  16. 进销存设计中的库存设计
  17. 树莓派控制超声波传感器
  18. PHP判断ip是否在指定IP段内(微信支付回调IP段验证)
  19. 运行浏览器无痕模式报错
  20. WebXR 元宇宙或将基于 Web

热门文章

  1. Word - 为何在 Word 中打字时未到行尾就自动换行?
  2. 婚宴座位图html5,大型婚宴 婚礼座位安排(图)
  3. win7计算机各硬盘显示容量,Win7系统磁盘分区不显示容量怎么设置
  4. STM32F05x加入RDP(LV1)后,Segger无法Unlock的解决办法
  5. 修改RabbitMQ密码
  6. 利盟Lexmark Intuition S508 一体机驱动
  7. Oracle全文索引
  8. c语言 12 goto,C语言第12讲goto语句及习题课..ppt
  9. 微信小程序-预览图片识别二维码
  10. linux 查询hba卡类型,Centos系统查询hba卡型号