uwsgi相關配置
[uwsgi]
http = :80 # 對外提供http的服務的埠
master = true # 啟動主行程,來管理其他行程,其它的uwsgi行程都是這個master行程的子行程,如果kill這個master行程,相當于重啟所有的uwsgi行程,
socket = 127.0.0.1:8080 # 用于和nginx進行資料互動的埠
listen = 120 #設定socket的監聽佇列大小(默認:100)
chdir = /home/project/app # 程式的主目錄(django)
wsgi-file = app/wsgi.py # 程式的wsgi的目錄
max-requests = 5000 #為每個作業行程設定請求數的上限,當一個作業行程處理的請求數達到這個值,那么該作業行程就會被回收重用(重啟),你可以使用這個選項來默默地對抗記憶體泄漏
buffer-size = 32768 #設定用于uwsgi包決議的內部快取區大小為64k,默認是4k,
vacuum = true #當服務器退出的時候自動洗掉unix socket檔案和pid檔案,
processes = 100 # 最大作業行程數
threads = 10 # 每個作業行程中啟動的執行緒數
enable-threads = true #允許用內嵌的語言啟動執行緒,這將允許你在app程式中產生一個子執行緒
cheaper = 10
idle = 3600 #在經過sec秒的不活躍狀態的行程會被銷毀(進入了cheap模式),并最少保留cheaper指定的行程數
stats = 127.0.0.1:9000 # 通過該埠可以監控 uwsgi 的負載情況
harakiri = 60 #請求超時時間
harakiri-verbose = true #當一個請求被harakiri殺掉會,會輸出一條日志
daemonize = /home/opadm/log/uwsgi.log # 后臺運行,并輸出日志
log-maxsize = 5000000 #設定最大日志檔案大小
nginx檔案結構
... #全域塊
events { #events塊
...
}
http #http塊
{
... #http全域塊
server #server塊
{
... #server全域塊
location [PATTERN] #location塊
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全域塊
}
1、全域塊:配置影響nginx全域的指令,一般有運行nginx服務器的用戶組,nginx行程pid存放路徑,日志存放路徑,組態檔引入,允許生成worker process數等,
#Nginx的worker行程運行用戶以及用戶組
#user nobody nobody;
#Nginx開啟的行程數
worker_processes 1;
#worker_processes auto;
#以下引數指定了哪個cpu分配給哪個行程,一般來說不用特殊指定,如果一定要設的話,用0和1指定分配方式.
#這樣設就是給1-4個行程分配單獨的核來運行,出現第5個行程是就是隨機分配了,eg:
#worker_processes 4 #4核CPU
#worker_cpu_affinity 0001 0010 0100 1000;
nets
#定義全域錯誤日志定義型別,[debug|info|notice|warn|crit]
#error_log logs/error.log info;
#指定行程ID存盤檔案位置
#pid logs/nginx.pid;
#一個nginx行程打開的最多檔案描述符數目,理論值應該是最多打開檔案數(ulimit -n)與nginx行程數相除,但是nginx分配請求并不是那么均勻,所以最好與ulimit -n的值保持一致,
worker_rlimit_nofile 65535;
2、events塊:配置影響nginx服務器或與用戶的網路連接,有每個行程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網路連接序列化等,(驚群,大致意思是,當某一時刻只有一個網路連接到來時,多個睡眠行程會被同時叫醒,但只有一個行程能夠獲得連接,如果每次喚醒的行程數目太多,會影響部分系統性能,nginx行程接收的連接進行序列化,防止多個行程對連接的爭搶,)
events {
#use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網路I/O模型,如果跑在FreeBSD上面,就用kqueue模型,
use epoll;
#每個行程可以處理的最大連接數,理論上每臺nginx服務器的最大連接數為worker_processes*worker_connections,理論值:worker_rlimit_nofile/worker_processes
#注意:最大客戶數也由系統的可用socket連接數限制(~ 64K),所以設定不切實際的高沒什么好處
worker_connections 65535;
#worker作業方式:串行(一定程度降低負載,但服務器吞吐量大時,關閉使用并行方式)
#multi_accept on;
}
3、http塊:可以嵌套多個server,配置代理,快取,日志定義等絕大多數功能和第三方模塊的配置,如檔案引入,mime-type定義,日志自定義,是否使用sendfile傳輸檔案,連接超時時間,單連接請求數等,
#檔案擴展名與檔案型別映射表
include mime.types;
#默認檔案型別
default_type application/octet-stream;
#日志相關定義
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#定義日志的格式,后面定義要輸出的內容,
#1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
#2.$remote_user :用來記錄客戶端用戶名稱;
#3.$time_local :用來記錄訪問時間與時區;
#4.$request :用來記錄請求的url與http協議;
#5.$status :用來記錄請求狀態;
#6.$body_bytes_sent :記錄發送給客戶端檔案主體內容大小;
#7.$http_referer :用來記錄從那個頁面鏈接訪問過來的;
#8.$http_user_agent :記錄客戶端瀏覽器的相關資訊
#連接日志的路徑,指定的日志格式放在最后,
#access_log logs/access.log main;
#只記錄更為嚴重的錯誤日志,減少IO壓力
error_log logs/error.log crit;
#關閉日志
#access_log off;
#默認編碼
#charset utf-8;
#服務器名字的hash表大小
server_names_hash_bucket_size 128;
#客戶端請求單個檔案的最大位元組數
client_max_body_size 8m;
#指定來自客戶端請求頭的hearerbuffer大小
client_header_buffer_size 32k;
#指定客戶端請求中較大的訊息頭的快取最大數量和大小,
large_client_header_buffers 4 64k;
#開啟高效傳輸模式,
sendfile on;
#防止網路阻塞
tcp_nopush on;
tcp_nodelay on;
#客戶端連接超時時間,單位是秒
keepalive_timeout 60;
#客戶端請求頭讀取超時時間
client_header_timeout 10;
#設定客戶端請求主體讀取超時時間
client_body_timeout 10;
#回應客戶端超時時間
send_timeout 10;
#FastCGI相關引數是為了改善網站的性能:減少資源占用,提高訪問速度,
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip模塊設定
#開啟gzip壓縮輸出
gzip on;
#最小壓縮檔案大小
gzip_min_length 1k;
#壓碩訓沖區
gzip_buffers 4 16k;
#壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
gzip_http_version 1.0;
#壓縮等級 1-9 等級越高,壓縮效果越好,節約寬帶,但CPU消耗大
gzip_comp_level 2;
#壓縮型別,默認就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn,
gzip_types text/plain application/x-javascript text/css application/xml;
#前端快取服務器快取經過壓縮的頁面
gzip_vary on;
4、server塊:配置虛擬主機的相關引數,一個http中可以有多個server,
#虛擬主機定義
server {
#監聽埠
listen 80;
#訪問域名
server_name localhost;
#編碼格式,若網頁格式與此不同,將被自動轉碼
#charset koi8-r;
#虛擬主機訪問日志定義
#access_log logs/host.access.log main;
#對URL進行匹配
location / {
#訪問路徑,可相對也可絕對路徑
root html;
#首頁檔案,以下按順序匹配
index index.html index.htm;
}
#錯誤資訊回傳頁面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#訪問URL以.php結尾則自動轉交給127.0.0.1
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#php腳本請求全部轉發給FastCGI處理
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
#禁止訪問.ht頁面 (需ngx_http_access_module模塊)
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#HTTPS虛擬主機定義
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#vue配置
server {
listen 80;
server_name jcsd-cdn-monitor.jdcloud.com;
#charset koi8-r;
#access_log logs/host.access.log main;
root /root/dist;
location / {
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
5、location塊:配置請求的路由,以及各種頁面的處理情況,
#Nginx運行狀態,StubStatus模塊獲取Nginx自啟動的作業狀態(編譯時要開啟對應功能)
#location /NginxStatus {
# #啟用StubStatus的作業訪問狀態
# stub_status on;
# #指定StubStaus模塊的訪問日志檔案 可off
# access_log logs/Nginxstatus.log;
# #Nginx認證機制(需Apache的htpasswd命令生成)
# #auth_basic "NginxStatus";
# #用來認證的密碼檔案
# #auth_basic_user_file ../htpasswd;
#}
訪問:http://IP/NginxStatus(測驗就不加密碼驗證相關)
反向代理:
#以下配置追加在HTTP的全域變數中
#啟動代理快取功能
proxy_buffering on;
#nginx跟后端服務器連接超時時間(代理連接超時)
proxy_connect_timeout 5;
#后端服務器資料回傳時間(代理發送超時)
proxy_send_timeout 5;
#連接成功后,后端服務器回應時間(代理接收超時)
proxy_read_timeout 60;
#設定代理服務器(nginx)保存用戶頭資訊的緩沖區大小
proxy_buffer_size 16k;
#proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設定
proxy_buffers 4 32k;
#高負荷下緩沖大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;
#設定快取檔案夾大小,大于這個值,將從upstream服務器傳
proxy_temp_file_write_size 64k;
#反向代理快取目錄
proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;
#levels=1:2 設定目錄深度,第一層目錄是1個字符,第2層是2個字符
#keys_zone:設定web快取名稱和記憶體快取空間大小
#inactive:自動清除快取檔案時間,
#max_size:硬碟空間最大可使用值,
#指定臨時快取檔案的存盤路徑(必須在同一磁區)
proxy_temp_path /data/proxy/temp;
#服務配置
server {
#偵聽的80埠
listen 80;
server_name localhost;
location / {
#反向代理快取設定命令(proxy_cache zone|off,默認關閉所以要設定)
proxy_cache cache_one;
#對不同的狀態碼快取不同時間
proxy_cache_valid 200 304 12h;
#設定以什么樣引數獲取快取檔案名
proxy_cache_key $host$uri$is_args$args;
#后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
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_pass http://IP;
#檔案過期時間控制
expires 1d;
}
#配置手動清楚快取(實作此功能需第三方模塊 ngx_cache_purge)
#http://www.123.com/2017/0316/17.html訪問
#http://www.123.com/purge/2017/0316/17.html清楚URL快取
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#設定擴展名以.jsp、.php、.jspx結尾的動態應用程式不做快取
location ~.*\.(jsp|php|jspx)?$ {
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_pass http://IP;
}
負載均衡:
#負載均衡服務器池
upstream my_server_pool {
#調度演算法
#1.輪循(默認)(weight輪循權值)
#2.ip_hash:根據每個請求訪問IP的hash結果分配,(會話保持)
#3.fair:根據后端服務器回應時間最短請求,(upstream_fair模塊)
#4.url_hash:根據訪問的url的hash結果分配,(需hash軟體包)
#引數:
#down:表示不參與負載均衡
#backup:備份服務器
#max_fails:允許最大請求錯誤次數
#fail_timeout:請求失敗后暫停服務時間,
server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30;
}
#負載均衡呼叫
server {
...
location / {
proxy_pass http://my_server_pool;
}
}
ip限制:
#限制IP訪問
location / {
deny 192.168.0.2; #拒絕的ip
allow 192.168.0.0/24;
allow 192.168.1.1;
deny all;
}
示例:
#user administrator administrators; #配置用戶或者組,默認為nobody nobody,
#worker_processes 2; #允許生成的行程數,默認為1
worker_connections 204800; #每個作業行程的最大連接數量,根據硬體調整,和前面作業行程配合起來用,盡量大,但是別把cpu跑到100%就行,每個行程允許的最多連接數,理論上每臺nginx服務器的最大連接數為,
client_header_buffer_size 4k; #客戶端請求頭部的緩沖區大小,
#pid /nginx/pid/nginx.pid; #指定nginx行程運行檔案存放地址
error_log log/error.log debug; #制定日志路徑,級別,這個設定可以放入全域塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設定網路連接序列化,防止驚群現象發生,默認為on
multi_accept on; #設定一個行程是否同時接受多個網路連接,默認為off
#use epoll; #事件驅動模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數,默認為512
}
http {
include mime.types; #設定mime型別,型別由mime.type檔案定義
default_type application/octet-stream; #默認檔案型別,默認為text/plain
#access_log off; #取消服務日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日志格式的默認值
sendfile on; #允許sendfile方式傳輸檔案,默認為off,可以在http塊,server塊,location塊,
sendfile_max_chunk 100k; #每個行程每次呼叫傳輸數量不能大于設定的值,默認為0,即不設上限,
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊,
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備(從廣義上講,就是對于重要的服務,使用兩臺服務器,互相備份,共同執行同一服務,當一臺服務器出現故障時,可以由另一臺服務器承擔服務任務,從而在不需要人工干預的情況下,自動保證系統能持續提供服務,雙機熱備由備用的服務器解決了在主服務器故障時服務不中斷的問題)
}
error_page 404 https://www.baidu.com; #錯誤頁
server {
keepalive_requests 120; #單連接請求上限次數,
listen 4545; #監聽埠
server_name 127.0.0.1; #監聽地址
location ~*^.+$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫,
#root path; #根目錄
#index vv.txt; #設定默認頁
proxy_pass http://mysvr; #請求轉向mysvr 定義的服務器串列
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357271.html
標籤:其他
