環境:CentOS Linux release 7.2.1511 (Core)
python2.7
1、安裝nginx
1.安裝nginx依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.下載nginx壓縮包(版本自行修改)
wget http://nginx.org/download/nginx-1.8.0.tar.gz
3.解壓
tar -zxvf nginx-1.8.0.tar.gz
4.進入目錄編譯安裝
cd nginx-1.8.0
./configure
make && make install
等待安裝完成即可,一般默認路徑在/usr/local/nginx-1.8.0
安裝gunicorn
pip install gunicorn
進入項目目錄,編寫uwsgi.ini文件
[uwsgi]
#使用nginx連接時使用
socket=127.0.0.1:9099
#直接做web服務器使用
#http=127.0.0.1:9091
#項目目錄
chdir=/root/mgad3/mgad/mgad_data
#項目中wsgi.py文件的目錄,相對於項目目錄
wsgi-file=/mgad_data/wsgi.py
processes=4
threads=2
master=True
pidfile=uwsgi.pid
daemonize=uwsgi.log
我這裏用的是gunicorn起服務,nginx轉發到gunicorn中,所以在manage.py的目錄裏用:
1.後臺執行,輸出在log中,-w:開啓4個併發量,-D:後臺執行,-t:超時時間(秒),上傳大文件時記得帶上,否則會因爲超時,gunicorn自動kill掉進程。
gunicorn --bind 172.x.x.x:8090 -w=4 -D --access-logfile ./logs/log mgad_data.wsgi:application
2.直接執行,輸出在控制檯
gunicorn --bind 172.x.x.x:8090 -w=4 --access-logfile ./logs/log mgad_data.wsgi:application
配置nginx
在/usr/local/nginx-1.8.0/conf/nginx.cong中
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8008; # 監聽端口,隨意改
server_name crawler.taobao1.com; # 向外開放的域名,可以是www.xxx.com或者172.x.x.x的類型(ip)
location / {
proxy_pass http://172.x.x.x:8090; # 將所有的內容轉發到gunicorn所開放的ip和端口
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
如果你想通過www.baotao1.com訪問自己的接口,在linux中,修改host映射文件就行,只需兩步
1.vim /etc/host 然後添加一行:
www.taobao1.com 172.x.x.x(你的ip)
2.vim /etc/hosts
172.x.x.x www.taobao1.com
啓動nginx
cd /usr/local/nginx-1.8.0
sudo /sbin/nginx
停止nginx
sudo /sbin/nginx -s stop
然後就可以通過這種方式訪問了
curl 'http://www.taobao1.com:8090/index' -X POST -d '{"user_type":2,"use_mq":"true","kol_id":"58119178989","platform":22,"add_user":1, "level": "P7"}'
如果想在公網上訪問你的專屬域名,是要單獨購買的,這個只能在局域網中使用