python django nginx配置

本文介绍了如何在Linux系统中安装并配置Nginx服务器,指向Django应用,设置静态文件访问,以及使用uwsgi管理Django应用的详细步骤,包括创建用户、启动脚本和uwsgi配置参数。
摘要由CSDN通过智能技术生成

1、装好nginx,启动

/usr/local/nginx/sbin/nginx -c /home/drun/resmanagerweb/resmanagerweb.conf
 cat resmanagerweb.conf|grep -Ev '^[[:space:]].*#|^#|^$' 

user  root;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        access_log /home/drun/log/nginx_access.log;
        error_log /home/drun/log/nginx_error.log;
        client_max_body_size 75M;
        location /static {
            alias /home/drun/resmanagerweb/static;
        }
        location / {
            alias /home/drun/resmanagerweb;
                include /usr/local/nginx/conf/uwsgi_params;
                uwsgi_read_timeout 3600;
                uwsgi_pass 127.0.0.1:9099;
        }
        access_log off;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
} 

2、创建一个用户比如drun用于专门存放django工程

3、配置启动脚本

cd ~/bin
$ cat startapp.sh
. ~/.bash_profile
cd
source venv/bin/activate
uwsgi --ini uwsgi.ini

4、uwsgi.ini

[uwsgi]
socket = 127.0.0.1:9099
chdir=/home/drun/resmanagerweb
module=resmanagerweb.wsgi
master = true
processes=2
threads=2
max-requests=2000
chmod-socket=664
vacuum=true
daemonize = /home/drun/uwsgi.log
DjangoNginx 结合使用可以帮助提高Web应用的性能和安全性。Nginx通常作为反向代理服务器,处理静态文件请求,而Django负责动态内容的生成。以下是基本的配置步骤: 1. 安装 NginxDjango: - Ubuntu/Debian: `sudo apt-get install nginx python3-django` - CentOS/RHEL: `sudo yum install nginx python3-pip` 2. 设置 Django 项目结构: - 创建一个新的Django项目并启动它。 - 在项目的`settings.py`中配置WSGI服务器(如`wsgi.py`),用于Nginx连接。 3. Nginx 配置模板(假设你的网站根目录是 `/var/www/html/myproject`): ```nginx server { listen 80; # 或者443如果需要HTTPS server_name example.com; location /static/ { alias /path/to/project/static/; } location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 这里将静态文件路径指向了`/path/to/project/static/`,并将所有其他请求转发给本地运行的Django应用。 4. 启动Nginx服务: `sudo service nginx start` 5. 更新 Django 的 WSGI 配置: 在`myproject/wsgi.py`中添加类似这样的内容: ```python from django.core.wsgi import get_wsgi_application application = get_wsgi_application() ``` 然后,在`myproject/settings.py`中设置WSGI_URL_scheme为`http`(如果是HTTPS则为`https`)。 6. 开启Django应用: `python manage.py runserver 0.0.0.0:8000` (如果你使用的是Python3)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值