前面讲到了使用Nginx+uWSGI部署python项目的时候http://www.javashuo.com/article/p-qhwkhfgf-m.html,Nginx被用来作反向代理、动静分离的做用,若是当服务器的请求并发太高,单机服务器的压力过大,就须要使用负载均衡,让更多的服务器去分摊之前一个服务器的压力。这里简单讲一下Nginx用做负载均衡,反向代理,以及动静分离的相关配置。javascript
找到nginx.conf文件位置,centos6x系统默认位置在/etc/nginx,若是安装了wdcp,nginx.conf文件位置在/www/wdlinux/nginx/conf下面php
vi /etc/nginx/nginx.confcss
1 # max_clients = worker_processes * worker_connections(nginx做为http服务器的时候,做为反向代理服务器须要/2)
2 worker_processes auto; 3 worker_cpu_affinity auto; 4 worker_rlimit_nofile 65535; # 进程的最大打开文件数限制。这样nginx就不会有“too many open files”问题了(能够设置高并发使用)
5
6 # 全局错误日志定义类型[ debug | info | notice | warn | error | crit ],越后面错误级别越高
7 error_log /var/log/nginx/error.log notice; # nginx错误日志地址
8 #进程文件
9 pid /var/run/nginx.pid; 10
11 events {
# use epoll; 12 worker_connections 10240; # 单个进程最大链接数,最大为65535,受worker_rlimit_nofile有影响)
13 } 14
15 http { 16
17 include /etc/nginx/mime.types; # 文件扩展名与文件类型映射表
18 default_type application/octet-stream; # 默认文件类型
19 charset utf-8; # 默认编码
20 fastcgi_intercept_errors on; # 开启nginx自定义设置,好比后面的error_page会使用
21
22 gzip on; # 开启gzip压缩输出
23 gzip_min_length 1k; # 最小压缩文件大小
24 gzip_buffers 4 16k; # 压缩缓冲区
25 gzip_http_version 1.0; # 压缩版本(默认1.1,前端若是是squid2.5请使用1.0)
26 gzip_comp_level 2; # 压缩等级
27 gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持压缩的类型
28 gzip_vary on; 29
30 # tcp优化
31 sendfile on; 32 tcp_nopush on; 33 tcp_nodelay on; 34 keepalive_timeout 120; # 长链接超时时间,单位是秒
35
36 # limit_zone zone_name $binary_remote_addr 10m; # 控制一个ip多并发的个数,zone_name是名字,10m是记录区空间,使用方法在location下添加(limit_conn zone_name 1; # 最多一个并发)
37
38 # client_header_buffer_size 2k; # 客户请求头缓冲大小 nginx默认会用client_header_buffer_size这个buffer来读取header值,若是 header过大,它会使用large_client_header_buffers来读取
39 # large_client_header_buffers 4 16k; # 默认为4K,请求行超过设置的第一个数4,请求的Header头信息大于设置的第二个数16k,会报"Request URI too large"(414)或“Bad request”(400)
40 # client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
41 # client_max_body_size 8m; # 请求体body最大大小
42 # client_header_timeout 60s; # 设置nginx读取客户端请求Header头信息的超时时间,若是超过该指令设置的时间,nginx将返回"Requet time out"错误信息(HTTP的408错误码)
43 # client_body_timeout 60s; # 设定nginx读取客户端请求内容的超时时间,若是超过该指令设置的时间,nginx将返回"Request time out"错误信息(HTTP状态码408)
44 # send_timeout 60s; # 设置发送给客户端的应答超时时间。指两次tcp握手,尚未转为established状态的时间。若是这个时间,客户端没有响应,Nginx则关闭链接
45
46 # 动态请求服务器池
47 upstream api_server { 48 ip_hash; 49 # server unix:/www/web/project_name/socket.sock; # 使用该服务器socket通讯的方式,比http服务器开销更小
50 server 192.168.2.1:8001 weight=1; # weight越大,负载的权重就越大
51 server 192.168.2.2:8001 weight=1; 52 } 53
54 # 静态请求服务器池
55 # upstream static_server {
56 # server 192.168.2.3:80;
57 # }
58
59 server { 60 listen 80; # 监听的端口号
61 server_name www.zzq.com www.gmx.com; # ip或者域名
62 error_log /www/web/logs/nginx_error.log; # 错误日志地址
63 access_log /www/web/logs/nginx.log; # 访问记录地址
64 error_page 404 403 500 502 503 /404.html; # 重定向nginx的错误页面
65
66 # 重定向自定义的页面
67 location = /404.html { 68 root /www/web/project_name/templates; # html地址
69 } 70 # 防止盗链,减小服务器压力
71 location ~* \.(jpg|jpeg|bmp|gif|png|css|js|ico|webp|tiff|ttf|svg|woff|woff2) { 72 valid_referers none blocked *.xxx.com xxx.com ~\.google\. ~\.bing\. ~\.baidu\.; # 能够访问的网站(后面为谷歌,百度,必应等)
73 if ($invalid_referer) { 74 return 403; # 也能够直接返回一个禁止盗链的提示
75 } 76 root /www/web/project_name; # 使用nginx作动静分离,减小uwsgi服务器压力
77 expires 30d; 78 access_log off; # 不须要写入访问日志中
79 } 80 #设定查看Nginx状态的地址
81 location /NginxStatus { 82 stub_status on; 83 access_log on; 84 auth_basic "NginxStatus"; 85 auth_basic_user_file conf/htpasswd; 86 } 87 location /robots.txt { 88 root /www/web/project_name/static; 89 access_log off; # 不须要写入访问日志中
90 log_not_found off; # 是否在error_log中记录不存在的错误。默认是
91 } 92 location /favicon.ico { 93 root /www/web/project_name/static; 94 access_log off; # 不须要写入访问日志中
95 log_not_found off; # 是否在error_log中记录不存在的错误。默认是
96 } 97 # 启动反向代理
98 location / { 99
100 # uwsgi_pass使用uwsgi协议。proxy_pass使用普通的HTTP与uWSGI服务器联系。uWSGI文档声称该协议更好,更快,而且能够受益于全部uWSGI特殊功能
101 include uwsgi_params; 102 uwsgi_pass api_server; 103
104 # uwsgi链接的时候配置
105 uwsgi_read_timeout 600; # 指定接收uWSGI应答的超时时间,完成握手后接收uWSGI应答的超时时间
106 uwsgi_connect_timeout 600; # 指定链接到后端uWSGI的超时时间
107 uwsgi_send_timeout 600; # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间
108
109 # 使用http协议配置方法
110 # proxy_pass http://api_server; # 这里http后等于第一行配置的名字
111 # proxy_redirect off;
112 # proxy_set_header Host $host;
113 # proxy_set_header X-Real-IP $remote_addr;
114 # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
115
116 # nginx用做代理的时候配置(和http链接方法一块儿使用)
117 # proxy_send_timeout 600; # 完成握手后向代理请求的超时时间
118 # proxy_connect_timeout 600; # 指定链接到后端uWSGI的超时时间
119 # proxy_read_timeout 600; # 完成握手后接收代理应答的超时时间
120 } 121 } 122 include /etc/nginx/conf.d/*.conf; # 同时也加载其余nginx的配置文件
123 }
在根据本身项目需求配置好nginx配置之后,重启nginx便可html