博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx + uwsgi 跑python应用
阅读量:5151 次
发布时间:2019-06-13

本文共 6303 字,大约阅读时间需要 21 分钟。

安装nginx:

yum -y install nginx

编辑:vim /etc/nginx/nginx.conf

user root root;

worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
worker_rlimit_nofile 65535;

include /usr/share/nginx/modules/*.conf;

events {

worker_connections 65535;
multi_accept on;
use epoll;
}

http {

include mime.types;
default_type application/octet-stream;
server_tokens off;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 20m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 30;

log_format main "[$time_local] - " "$request - " "$status - " "$request_time - " "$body_bytes_sent - " "$http_referer - " "$sent_http_location - " "$http_user_agent - " "$remote_addr - " "$http_x_forwarded_for - " "$upstream_addr - " "$upstream_status - " "$upstream_response_time - ";

access_log /var/log/nginx/access.log main;

gzip on;

gzip_comp_level 2;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
gzip_proxied any;
gzip_vary on;

proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=tmp_cache:1024m inactive=20m max_size=1024m;

add_header X-Frame-Options SAMEORIGIN;
proxy_next_upstream error timeout;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;

upstream XXX-admin {

ip_hash;
server 192.168.1.1:8001 max_fails=3 fail_timeout=30s;
server 192.168.1.2:8001 max_fails=3 fail_timeout=30s;
}
upstream socketio_nodes {
ip_hash;
server 192.168.1.1:5555;
server 192.168.1.1:5556;
server 192.168.1.1:5557;
server 192.168.1.1:5558;
server 192.168.1.2:5555;
server 192.168.1.2:5556;
server 192.168.1.2:5557;
server 192.168.1.2:5558;
}

include /etc/nginx/conf.d/*.conf;

}

 

编辑系统配置文件:vim /etc/nginx/conf.d/XXX.conf 

map $http_upgrade $connection_upgrade {

default upgrade;
'' close;
}

server {

listen *:80;
server_name *.XXXXX.com *.XXXXX.com.cn;
access_log /var/log/nginx/XXX.access.log main;
error_log /var/log/nginx/XXX.error.log;
root /app/XXX/XXX_web/dist;
index index.html;

add_header Access-Control-Allow-Origin http://XXX.XXXXX.com;

add_header Access-Control-Allow-Headers Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS,HEAD,PUT,DELETE;
add_header Access-Control-Allow-Credentials true;

location / {

root /app/XXX/XXX_web/dist;
index index.html;
try_files $uri $uri/ /index.html last;
}

location ^~ /api {

uwsgi_pass XXX-admin;
uwsgi_send_timeout 600;
uwsgi_connect_timeout 600;
uwsgi_read_timeout 600;
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /app/XXX/venv;
uwsgi_param UWSGI_CHDIR /app/XXX;
uwsgi_param UWSGI_SCRIPT XXX:app;
uwsgi_param SCRIPT_NAME "";
}

location /socket.io {

proxy_pass http://socketio_nodes/socket.io;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

}

}

server {

listen 80;
server_name _;
access_log /var/log/nginx/nginx.log main;
error_log /var/log/nginx/nginx.err;
return 400;
}

把文件中的XXXXX换成你的域名,把XXX换成你的系统名称

手动安装uwsgi服务

  1. 先决条件
  • 安装了python
  • 安装了python-devel
  • 安装了pip
  1. 获取uwsgi最新的源码包: uwsgi-2.0.17.1.tar.gz
  2. pip install uwsgi-2.0.17.1.tar.gz
  3. 创建 /usr/lib/systemd/system/uwsgi.service 文件,内容为

[Unit]

Description=uWSGI Emperor Service

After=syslog.target

 

[Service]

EnvironmentFile=-/etc/sysconfig/uwsgi

ExecStartPre=/bin/mkdir -p /run/uwsgi

ExecStartPre=/bin/chown uwsgi:uwsgi /run/uwsgi

ExecStart=/usr/bin/uwsgi  --ini /etc/uwsgi.ini

ExecReload=/bin/kill -HUP $MAINPID

KillSignal=SIGINT

Restart=always

Type=notify

StandardError=syslog

NotifyAccess=all

 

[Install]

WantedBy=multi-user.target

 

  1. 新建 /etc/uwsgi.d/ 文件夹, /etc/uwsgi.ini 文件

     

[uwsgi]

uid = root

gid = root

socket = /var/run/uwsgi/uwsgi.socket

pidfile = /var/run/uwsgi/uwsgi.pid

emperor = /etc/uwsgi.d

#emperor-tyrant = true

master = true

autoload = true

log-date = true

logto = /var/log/uwsgi.log

  1. XXX相关配置

1)    /etc/nginx/conf.d/XXX.conf

map $http_upgrade $connection_upgrade {

default upgrade;
'' close;
}

server {

listen *:80;
server_name *.XXXXX.com *.XXXXX.com.cn;
access_log /var/log/nginx/XXX.access.log main;
error_log /var/log/nginx/XXX.error.log;
root /app/XXX/XXX_web/dist;
index index.html;

add_header Access-Control-Allow-Origin http://XXX.XXXXX.com;

add_header Access-Control-Allow-Headers Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS,HEAD,PUT,DELETE;
add_header Access-Control-Allow-Credentials true;

location / {

root /app/XXX/XXX_web/dist;
index index.html;
try_files $uri $uri/ /index.html last;
}

location ^~ /api {

uwsgi_pass XXX-admin;
uwsgi_send_timeout 600;
uwsgi_connect_timeout 600;
uwsgi_read_timeout 600;
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /app/XXX/venv;
uwsgi_param UWSGI_CHDIR /app/XXX;
uwsgi_param UWSGI_SCRIPT XXX:app;
uwsgi_param SCRIPT_NAME "";
}

location /socket.io {

proxy_pass http://socketio_nodes/socket.io;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

}

}

server {

listen 80;
server_name _;
access_log /var/log/nginx/nginx.log main;
error_log /var/log/nginx/nginx.err;
return 400;
}

2)    /etc/uwsgi.d/XXX.ini

[uwsgi]

chdir = /app/XXX/
wsgi-file = /app/XXX/XXX.py
home = /app/XXX/venv

http-websockets = true

virtualenv = /app/XXX/venv
callable = app
vacuum = true

uwsgi-socket = 0.0.0.0:8001

socket = /run/uwsgi/XXX.sock

buffer-size = 65535

master = false

enable-threads = true

processes = 4

threads = 2

thunder-lock = true

plugins = python

lazy-apps = true

3)    /etc/uwsgi.d/XXX-socket.ini

[uwsgi]

gevent=1000
gevent-monkey-patch=true
http=:5555
http=:5556
http=:5557
http=:5558
http-websockets=true
master=true
chdir=/app/XXX/
virtualenv=/app/XXX/venv
buffer-size=65535
thunder-lock=true
vacuum=true
wsgi-file=XXX_SOCKET.py
callable=app

 

本应用起了websocket,如果没有的可以去除相关配置

     

转载于:https://www.cnblogs.com/chmyee/p/9373377.html

你可能感兴趣的文章
5G边缘网络虚拟化的利器:vCPE和SD-WAN
查看>>
MATLAB基础入门笔记
查看>>
【UVA】434-Matty's Blocks
查看>>
Android开发技术周报 Issue#80
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
django知识点总结
查看>>
C++ STL stack、queue和vector的使用
查看>>
OAuth2 .net MVC实现获取token
查看>>
java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查
查看>>
使用Reporting Services时遇到的小问题
查看>>
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>
js兼容公用方法
查看>>
如何将应用完美迁移至Android P版本
查看>>
【转】清空mysql一个库中的所有表的数据
查看>>
基于wxPython的python代码统计工具
查看>>
淘宝JAVA中间件Diamond详解(一)---简介&快速使用
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>