在Django nginx gunicorn设置外部IP错误问题,怎么解决
1个回答
展开全部
通Nginx部署Django(基于ubuntu)
Django部署式采用nginx+uwsgi式其比较见种式
种式我通做nginx作服务器前端接收WEB所请求统管理请求nginx所静态请求自处理(NGINX强项)NGINX所非静态请求通uwsgi传递给Django由Django进行处理完WEB请求
见uwsgi作用类似桥接器起桥梁作用
Linux强项用做服务器所面整部署程我选择Ubuntu完
、安装Nginx
Nginx款轻量级Web 服务器/反向代理服务器及电邮件(IMAP/POP3)代理服务器并BSD-like 协议发行其特点占内存少并发能力强事实nginx并发能力确实同类型网页服务器表现较
Nginx同前非流行web服务器利用其部署Django我做简单介绍
Nginx官网:
打ubuntu控制台(ctrl+alt+t)利用Ubuntu仓库安装
fnngj@ubuntu:~$ sudo apt-get install nginx #安装
启Nginx:
fnngj@ubuntu:~$ /etc/init.d/nginx start #启
fnngj@ubuntu:~$ /etc/init.d/nginx stop #关闭
fnngj@ubuntu:~$ /etc/init.d/nginx restart #重启
修改Nginx默认端口号打/etc/nginx/nginx.conf 文件修改端口号
复制代码
server {
listen 8088; # 修改端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
复制代码
概文件36行位置默认80端口号改其端口号 8088默认80端口号容易其应用程序占用
通面命令重启nginx访问:http//127.0.0.1:8088/
现图说明Nginx启功
二、安装uwsgi
通pip安装uwsgi
root@ubuntu:/etc# python3 -m pip install uwsgi
测试uwsgi创建test.py文件:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
通uwsgi运行该文件
fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --wsgi-file test.py
接配置Django与uwsgi连接处假定我django项目位置:/home/fnngj/pydj/myweb
fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --chdir /home/fnngj/pydj/myweb/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
用选项:
http : 协议类型端口号
processes : 启进程数量
workers : 启进程数量等同于processes(官网说spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 指定址启状态服务(enable the stats server on the specified address)
threads : 运行线程由于GIL存我觉真没啥用(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存(enable master process)
daemonize : 使进程台运行并志打指定志文件或者udp服务器(daemonize uWSGI)实际用运行记录输本文件
pidfile : 指定pid文件位置记录主进程pid号
vacuum : 服务器退候自清理环境删除unix socket文件pid文件(try to remove all of the generated file/sockets)
三、Nginx+uwsgi+Django
接我要三者结合起首先罗列项目所需要文件:
myweb/
├── manage.py
├── myweb/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── myweb_uwsgi.ini
我通Django创建myweb项目目录myweb已经帮我 wsgi.py文件所我需要再创建myweb_uwsgi.ini配置文件即uwsgi支持种类型配置文件xmlini等处使用ini类型配置
复制代码
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /home/fnngj/pydj/myweb
# Django s wsgi file
module = myweb.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
复制代码
配置其实相于节通wsgi命令面跟堆参数式给文件化
socket 指定项目执行端口号
chdir 指定项目目录
module myweb.wsgi 理解于myweb_uwsgi.ini文件说与平级myweb目录目录wsgi.py文件
其几参数参考节参数介绍
接切换myweb项目目录通uwsgi命令读取myweb_uwsgi.ini文件启项目
复制代码
fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/
fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini
[uWSGI] getting INI configuration from myweb_uwsgi.ini
*** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2016] ***
compiled with version: 4.8.4 on 26 January 2016 06:14:41
os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015
nodename: ubuntu
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:37:06) [GCC 4.8.4]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x8b52dc0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 319920 bytes (312 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 7158)
spawned uWSGI worker 1 (pid: 7160, cores: 1)
spawned uWSGI worker 2 (pid: 7161, cores: 1)
spawned uWSGI worker 3 (pid: 7162, cores: 1)
spawned uWSGI worker 4 (pid: 7163, cores: 1)
复制代码
注意查看uwsgi启信息错要检查配置文件参数否设置误
再接要做修改nginx.conf配置文件打/etc/nginx/nginx.conf文件添加内容
复制代码
……
server {
listen 8099;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/fnngj/pydj/myweb/static/;
}
}
……
复制代码
listen 指定nginx代理uwsgi外端口号
server_name 网资料都设置网址(例wwwexamplecom)我设置网址访问所指定本机默认ip
进行配置候我问题直想通nginx底何uwsgi产关联现看概主要两行配置
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
include 必须指定uwsgi_params;uwsgi_pass指本机IP端口与myweb_uwsgi.ini配置文件必须直
现重新启nginx翻看面重启nginx命令访问:http//127.0.0.1:8099/
通IP端口号指向请求应该先nginx页面执行些请求看些请求终转uwsgi处理
Django部署式采用nginx+uwsgi式其比较见种式
种式我通做nginx作服务器前端接收WEB所请求统管理请求nginx所静态请求自处理(NGINX强项)NGINX所非静态请求通uwsgi传递给Django由Django进行处理完WEB请求
见uwsgi作用类似桥接器起桥梁作用
Linux强项用做服务器所面整部署程我选择Ubuntu完
、安装Nginx
Nginx款轻量级Web 服务器/反向代理服务器及电邮件(IMAP/POP3)代理服务器并BSD-like 协议发行其特点占内存少并发能力强事实nginx并发能力确实同类型网页服务器表现较
Nginx同前非流行web服务器利用其部署Django我做简单介绍
Nginx官网:
打ubuntu控制台(ctrl+alt+t)利用Ubuntu仓库安装
fnngj@ubuntu:~$ sudo apt-get install nginx #安装
启Nginx:
fnngj@ubuntu:~$ /etc/init.d/nginx start #启
fnngj@ubuntu:~$ /etc/init.d/nginx stop #关闭
fnngj@ubuntu:~$ /etc/init.d/nginx restart #重启
修改Nginx默认端口号打/etc/nginx/nginx.conf 文件修改端口号
复制代码
server {
listen 8088; # 修改端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
复制代码
概文件36行位置默认80端口号改其端口号 8088默认80端口号容易其应用程序占用
通面命令重启nginx访问:http//127.0.0.1:8088/
现图说明Nginx启功
二、安装uwsgi
通pip安装uwsgi
root@ubuntu:/etc# python3 -m pip install uwsgi
测试uwsgi创建test.py文件:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
通uwsgi运行该文件
fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --wsgi-file test.py
接配置Django与uwsgi连接处假定我django项目位置:/home/fnngj/pydj/myweb
fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --chdir /home/fnngj/pydj/myweb/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
用选项:
http : 协议类型端口号
processes : 启进程数量
workers : 启进程数量等同于processes(官网说spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 指定址启状态服务(enable the stats server on the specified address)
threads : 运行线程由于GIL存我觉真没啥用(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存(enable master process)
daemonize : 使进程台运行并志打指定志文件或者udp服务器(daemonize uWSGI)实际用运行记录输本文件
pidfile : 指定pid文件位置记录主进程pid号
vacuum : 服务器退候自清理环境删除unix socket文件pid文件(try to remove all of the generated file/sockets)
三、Nginx+uwsgi+Django
接我要三者结合起首先罗列项目所需要文件:
myweb/
├── manage.py
├── myweb/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── myweb_uwsgi.ini
我通Django创建myweb项目目录myweb已经帮我 wsgi.py文件所我需要再创建myweb_uwsgi.ini配置文件即uwsgi支持种类型配置文件xmlini等处使用ini类型配置
复制代码
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /home/fnngj/pydj/myweb
# Django s wsgi file
module = myweb.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
复制代码
配置其实相于节通wsgi命令面跟堆参数式给文件化
socket 指定项目执行端口号
chdir 指定项目目录
module myweb.wsgi 理解于myweb_uwsgi.ini文件说与平级myweb目录目录wsgi.py文件
其几参数参考节参数介绍
接切换myweb项目目录通uwsgi命令读取myweb_uwsgi.ini文件启项目
复制代码
fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/
fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini
[uWSGI] getting INI configuration from myweb_uwsgi.ini
*** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2016] ***
compiled with version: 4.8.4 on 26 January 2016 06:14:41
os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2015
nodename: ubuntu
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:37:06) [GCC 4.8.4]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x8b52dc0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 319920 bytes (312 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 7158)
spawned uWSGI worker 1 (pid: 7160, cores: 1)
spawned uWSGI worker 2 (pid: 7161, cores: 1)
spawned uWSGI worker 3 (pid: 7162, cores: 1)
spawned uWSGI worker 4 (pid: 7163, cores: 1)
复制代码
注意查看uwsgi启信息错要检查配置文件参数否设置误
再接要做修改nginx.conf配置文件打/etc/nginx/nginx.conf文件添加内容
复制代码
……
server {
listen 8099;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/fnngj/pydj/myweb/static/;
}
}
……
复制代码
listen 指定nginx代理uwsgi外端口号
server_name 网资料都设置网址(例wwwexamplecom)我设置网址访问所指定本机默认ip
进行配置候我问题直想通nginx底何uwsgi产关联现看概主要两行配置
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
include 必须指定uwsgi_params;uwsgi_pass指本机IP端口与myweb_uwsgi.ini配置文件必须直
现重新启nginx翻看面重启nginx命令访问:http//127.0.0.1:8099/
通IP端口号指向请求应该先nginx页面执行些请求看些请求终转uwsgi处理
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |