Nginx詳細配置

前面講到了使用Nginx+uWSGI部署python項目的時候http://www.javashuo.com/article/p-qhwkhfgf-m.html,Nginx被用來作反向代理、動靜分離的做用,若是當服務器的請求併發太高,單機服務器的壓力過大,就須要使用負載均衡,讓更多的服務器去分攤之前一個服務器的壓力。這裏簡單講一下Nginx用做負載均衡,反向代理,以及動靜分離的相關配置。javascript

Nginx配置文件

找到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

相關文章
相關標籤/搜索