一、Http代理,反向代理:做爲web服務器最經常使用的功能之一,尤爲是反向代理。javascript
Nginx在作反向代理時,提供性能穩定,而且可以提供配置靈活的轉發功能。Nginx能夠根據不一樣的正則匹配,採起不一樣的轉發策略,好比圖片文件結尾的走文件服務器,動態頁面走web服務器,只要你正則寫的沒問題,又有相對應的服務器解決方案,你就能夠爲所欲爲的玩。而且Nginx對返回結果進行錯誤頁跳轉,異常判斷等。若是被分發的服務器存在異常,他能夠將請求從新轉發給另一臺服務器,而後自動去除異常服務器。php
二、負載均衡css
Nginx提供的負載均衡策略有2種:內置策略和擴展策略。內置策略爲輪詢,加權輪詢,Ip hash。擴展策略,就天馬行空,只有你想不到的沒有他作不到的啦,你能夠參照全部的負載均衡算法,給他一一找出來作下實現。html
上3個圖,理解這三種負載均衡算法的實現java
Ip hash算法,對客戶端請求的ip進行hash操做,而後根據hash結果將同一個客戶端ip的請求分發給同一臺服務器進行處理,能夠解決session不共享的問題。 node
三、web緩存linux
Nginx能夠對不一樣的文件作不一樣的緩存處理,配置靈活,而且支持FastCGI_Cache,主要用於對FastCGI的動態程序進行緩存。配合着第三方的ngx_cache_purge,對制定的URL緩存內容能夠的進行增刪管理。nginx
1.默認的configweb
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { 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"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; 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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # 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; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # 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; # } #} }
2.nginx文件結構算法
main # 全局配置
events { # nginx工做模式配置
}
http { # http設置
....
server { # 服務器主機配置
....
location { # 路由配置
....
}
location path {
....
}
location otherpath {
....
}
}
server {
....
location {
....
}
}
upstream name { # 負載均衡配置
....
}
}
1)全局塊:配置影響nginx全局的指令。通常有運行nginx服務器的用戶組,nginx進程pid存放路徑,日誌存放路徑,配置文件引入,容許生成worker process數等。
2)events塊:配置影響nginx服務器或與用戶的網絡鏈接。有每一個進程的最大鏈接數,選取哪一種事件驅動模型處理鏈接請求,是否容許同時接受多個網路鏈接,開啓多個網絡鏈接序列化等。
3)http塊:能夠嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,鏈接超時時間,單鏈接請求數等。
4)server塊:配置虛擬主機的相關參數,一個http中能夠有多個server。
5)location塊:配置請求的路由,以及各類頁面的處理狀況。
6)upstream:用於進行負載均衡的配置
3.main塊
# user nobody nobody; worker_processes 2; # error_log logs/error.log # error_log logs/error.log notice # error_log logs/error.log info # pid logs/nginx.pid worker_rlimit_nofile 1024;
上述配置都是存放在main全局配置模塊中的配置項
4.event 模塊
event { worker_connections 1024; multi_accept on; use epoll; }
上述配置是針對nginx服務器的工做模式的一些操做配置
5.http模塊
做爲web服務器,http模塊是nginx最核心的一個模塊,配置項也是比較多的,項目中會設置到不少的實際業務場景,須要根據硬件信息進行適當的配置,常規狀況下,使用默認配置便可!
http { ## # 基礎配置 ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL證書配置 ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # 日誌配置 ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip 壓縮配置 ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # 虛擬主機配置 ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;
1) 基礎配置
sendfile on:配置on讓sendfile發揮做用,將文件的回寫過程交給數據緩衝去去完成,而不是放在應用中完成,這樣的話在性能提高有有好處 tc_nopush on:讓nginx在一個數據包中發送全部的頭文件,而不是一個一個單獨發 tcp_nodelay on:讓nginx不要緩存數據,而是一段一段發送,若是數據的傳輸有實時性的要求的話能夠配置它,發送完一小段數據就馬上能獲得返回值,可是不要濫用哦 keepalive_timeout 10:給客戶端分配鏈接超時時間,服務器會在這個時間事後關閉鏈接。通常設置時間較短,可讓nginx工做持續性更好 client_header_timeout 10:設置請求頭的超時時間 client_body_timeout 10:設置請求體的超時時間 send_timeout 10:指定客戶端響應超時時間,若是客戶端兩次操做間隔超過這個時間,服務器就會關閉這個連接 limit_conn_zone $binary_remote_addr zone=addr:5m :設置用於保存各類key的共享內存的參數, limit_conn addr 100: 給定的key設置最大鏈接數 server_tokens:雖然不會讓nginx執行速度更快,可是能夠在錯誤頁面關閉nginx版本提示,對於網站安全性的提高有好處哦 include /etc/nginx/mime.types:指定在當前文件中包含另外一個文件的指令 default_type application/octet-stream:指定默認處理的文件類型能夠是二進制 type_hash_max_size 2048:混淆數據,影響三列衝突率,值越大消耗內存越多,散列key衝突率會下降,檢索速度更快;值越小key,佔用內存較少,衝突率越高,檢索速度變慢
2) 日誌配置
access_log logs/access.log:設置存儲訪問記錄的日誌
error_log logs/error.log:設置存儲記錄錯誤發生的日誌
3) SSL證書加密
ssl_protocols:指令用於啓動特定的加密協議,nginx在1.1.13和1.0.12版本後默認是ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2,TLSv1.1與TLSv1.2要確保OpenSSL >= 1.0.1 ,SSLv3 如今還有不少地方在用但有很多被攻擊的漏洞。 ssl prefer server ciphers:設置協商加密算法時,優先使用咱們服務端的加密套件,而不是客戶端瀏覽器的加密套件
4) 壓縮配置
gzip 是告訴nginx採用gzip壓縮的形式發送數據。這將會減小咱們發送的數據量。 gzip_disable 爲指定的客戶端禁用gzip功能。咱們設置成IE6或者更低版本以使咱們的方案可以普遍兼容。 gzip_static 告訴nginx在壓縮資源以前,先查找是否有預先gzip處理過的資源。這要求你預先壓縮你的文件(在這個例子中被註釋掉了),從而容許你使用最高壓縮比,這樣nginx就不用再壓縮這些文件了(想要更詳盡的gzip_static的信息,請點擊這裏)。 gzip_proxied 容許或者禁止壓縮基於請求和響應的響應流。咱們設置爲any,意味着將會壓縮全部的請求。 gzip_min_length 設置對數據啓用壓縮的最少字節數。若是一個請求小於1000字節,咱們最好不要壓縮它,由於壓縮這些小的數據會下降處理此請求的全部進程的速度。 gzip_comp_level 設置數據的壓縮等級。這個等級能夠是1-9之間的任意數值,9是最慢可是壓縮比最大的。咱們設置爲4,這是一個比較折中的設置。 gzip_type 設置須要壓縮的數據格式。上面例子中已經有一些了,你也能夠再添加更多的格式。
5) 文件緩存配置
open_file_cache 打開緩存的同時也指定了緩存最大數目,以及緩存的時間。咱們能夠設置一個相對高的最大時間,這樣咱們能夠在它們不活動超過20秒後清除掉。
open_file_cache_valid 在open_file_cache中指定檢測正確信息的間隔時間。
open_file_cache_min_uses 定義了open_file_cache中指令參數不活動時間期間裏最小的文件數。
open_file_cache_errors 指定了當搜索一個文件時是否緩存錯誤信息,也包括再次給配置中添加文件。咱們也包括了服務器模塊,這些是在不一樣文件中定義的。若是你的服務器模塊不在這些位置,你就得修改這一行來指定正確的位置。
6.server模塊
srever模塊配置是http模塊中的一個子模塊,用來定義一個虛擬訪問主機,也就是一個虛擬服務器的配置信息
server { listen 80; server_name localhost 192.168.1.100; root /nginx/www; index index.php index.html index.html; charset utf-8; access_log logs/access.log; error_log logs/error.log; ...... }
核心配置信息以下:
server:一個虛擬主機的配置,一個http中能夠配置多個server
server_name:用力啊指定ip地址或者域名,多個配置之間用空格分隔
root:表示整個server虛擬主機內的根目錄,全部當前主機中web項目的根目錄
index:用戶訪問web網站時的全局首頁
charset:用於設置www/路徑中配置的網頁的默認編碼格式
access_log:用於指定該虛擬主機服務器中的訪問記錄日誌存放路徑
error_log:用於指定該虛擬主機服務器中訪問錯誤日誌的存放路徑
7.location塊
location模塊是nginx配置中出現最多的一個配置,主要用於配置路由訪問信息
在路由訪問信息配置中關聯到反向代理、負載均衡等等各項功能,因此location模塊也是一個很是重要的配置模塊
基本配置
location / { root /nginx/www; index index.php index.html index.htm; }
location /:表示匹配訪問根目錄
root:用於指定訪問根目錄時,訪問虛擬主機的web目錄
index:在不指定訪問具體資源時,默認展現的資源文件列表
反向代理配置方式
經過反向代理代理服務器訪問模式,經過proxy_set配置讓客戶端訪問透明化
location / { proxy_pass http://localhost:8888; proxy_set_header X-real-ip $remote_addr; proxy_set_header Host $http_host; }
uwsgi配置
wsgi模式下的服務器配置訪問方式
location / { include uwsgi_params; uwsgi_pass localhost:8888 }
8.upstream模塊
upstream模塊主要負責負載均衡的配置,經過默認的輪詢調度方式來分發請求到後端服務器
簡單的配置方式以下
upstream test { server 192.168.0.1 weight=5; ip_hash; server 192.168.0.2 weight=7; }
upstream name { ip_hash; server 192.168.1.100:8000; server 192.168.1.100:8001 down; server 192.168.1.100:8002 max_fails=3; server 192.168.1.100:8003 fail_timeout=20s; server 192.168.1.100:8004 max_fails=3 fail_timeout=20s; }
核心配置信息以下
ip_hash:指定請求調度算法,默認是weight權重輪詢調度,能夠指定
server host:port:分發服務器的列表配置
-- down:表示該主機暫停服務
-- max_fails:表示失敗最大次數,超過失敗最大次數暫停服務
-- fail_timeout:表示若是請求受理失敗,暫停指定的時間以後從新發起請求
9.nginx配置例子
########### 每一個指令必須有分號結束。################# #user administrator administrators; #配置用戶或者組,默認爲nobody nobody。 #worker_processes 2; #容許生成的進程數,默認爲1 #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; #文件擴展名與文件類型映射表 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 } } }
##安裝nginx yum -y install nginx ## 查看 Nginx 程序文件目錄:/usr/sbin/nginx $ ps -ef | grep nginx ## 查看 nginx.conf 配置文件目錄:/etc/nginx/nginx.conf $ nginx -t $ vim /etc/nginx/nginx.conf ## 配置文件目錄:/etc/nginx ## 虛擬主機配置文件目錄:/etc/nginx/sites-available/ ## 虛擬主機文件夾目錄:/var/www/,詳情可在 /etc/nginx/sites-available/ 中配置 ## 默認網頁文件目錄:/usr/share/nginx/html ## 測試配置文件,只檢查配置文件是否存在語法錯誤 $ nginx -t -c <path-to-nginx.conf> $ sudo nginx -t -c /etc/nginx/nginx.conf ## 啓動 Nginx 服務 $ nginx 安裝目錄 -c <path-to-nginx.conf> $ sudo /etc/init.d/nginx start ## 中止 Nginx 服務 $ sudo /usr/sbin/nginx -s stop ## 重啓 Nginx $ sudo /usr/sbin/nginx -s reload # 0.8 版本以後的方法 $ kill -HUP pid # 向 master 進程發送信號從容地重啓 Nginx,即服務不中斷 $ sudo service nginx start $ sudo service nginx stop $ sudo service nginx restart
Nginx 配置文件路徑:/etc/nginx/nginx.conf
## # 全局配置 ## user www-data; ## 配置 worker 進程的用戶和組 worker_processes auto; ## 配置 worker 進程啓動的數量,建議配置爲 CPU 核心數 error_log logs/error.log; ## 全局錯誤日誌 pid /run/nginx.pid; ## 設置記錄主進程 ID 的文件 worker_rlimit_nofile 8192; ## 配置一個工做進程可以接受併發鏈接的最大數 ## # 工做模式及鏈接數上限 ## events { # epoll 是多路複用 IO(I/O Multiplexing)中的一種方式, # 僅用於 Linux 2.6 以上內核,能夠大大提升 Nginx 性能 use epoll # 單個後臺 worker process 進程的最大併發連接數 # 併發總數 max_clients = worker_professes * worker_connections worker_connections 4096; ## Defaule: 1024 # multi_accept on; ## 指明 worker 進程馬上接受新的鏈接 } ## # http 模塊 ## http { ## # Basic Settings ## #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件, #對於普通應用,必須設爲 on, #若是用來進行下載等應用磁盤 IO 重負載應用,可設置爲 off, #以平衡磁盤與網絡 I/O 處理速度,下降系統的 uptime. sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; ## 鏈接超時時間 types_hash_max_size 2048; ## 指定散列類型表的最大大小 # server_tokens off; # server_names_hash_bucket_size 64; # this seems to be required for some vhosts # server_name_in_redirect off; include /etc/nginx/mime.types; ## 設定 mine 類型 default_type application/octet-stream; # 設定請求緩衝 client_header_buffer_size 128k; # 指定客戶端請求頭緩存大小,當請求頭大於 1KB 時會用到該項 large_client_header_buffers 4 128k; # 最大數量和最大客戶端請求頭的大小 ## # SSL Settings ## # 啓用全部協議,禁用已廢棄的不安全的SSL 2 和SSL 3 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE # 讓服務器選擇要使用的算法套件 ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; ## 訪問日誌 error_log /var/log/nginx/error.log; ## 錯誤日誌 ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; # 這個文件夾默認是空的 include /etc/nginx/sites-enabled/*; # 開啓的 Server 服務配置 } ## # mail 模塊 ## mail { # See sample authentication script at: # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # auth_http localhost/auth.php; # pop3_capabilities "TOP" "USER"; # imap_capabilities "IMAP4rev1" "UIDPLUS"; server { listen localhost:110; protocol pop3; proxy on; } server { listen localhost:143; protocol imap; proxy on; } }
nginx web配置例子
#運行用戶 user nobody; #啓動進程,一般設置成和cpu的數量相等 worker_processes 1; #全局錯誤日誌及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #工做模式及鏈接數上限 events { #epoll是多路複用IO(I/O Multiplexing)中的一種方式, #僅用於linux2.6以上內核,能夠大大提升nginx的性能 use epoll; #單個後臺worker process進程的最大併發連接數 worker_connections 1024; # 併發總數是 worker_processes 和 worker_connections 的乘積 # 即 max_clients = worker_processes * worker_connections # 在設置了反向代理的狀況下,max_clients = worker_processes * worker_connections / 4 爲何 # 爲何上面反向代理要除以4,應該說是一個經驗值 # 根據以上條件,正常狀況下的Nginx Server能夠應付的最大鏈接數爲:4 * 8000 = 32000 # worker_connections 值的設置跟物理內存大小有關 # 由於併發受IO約束,max_clients的值須小於系統能夠打開的最大文件數 # 而系統能夠打開的最大文件數和內存大小成正比,通常1GB內存的機器上能夠打開的文件數大約是10萬左右 # 咱們來看看360M內存的VPS能夠打開的文件句柄數是多少: # $ cat /proc/sys/fs/file-max # 輸出 34336 # 32000 < 34336,即併發鏈接總數小於系統能夠打開的文件句柄總數,這樣就在操做系統能夠承受的範圍以內 # 因此,worker_connections 的值需根據 worker_processes 進程數目和系統能夠打開的最大文件總數進行適當地進行設置 # 使得併發總數小於操做系統能夠打開的最大文件數目 # 其實質也就是根據主機的物理CPU和內存進行配置 # 固然,理論上的併發總數可能會和實際有所誤差,由於主機還有其餘的工做進程須要消耗系統資源。 # ulimit -SHn 65535 } http { #設定mime類型,類型由mime.type文件定義 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"'; access_log logs/access.log main; #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件, #對於普通應用,必須設爲 on, #若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off, #以平衡磁盤與網絡I/O處理速度,下降系統的uptime. sendfile on; #tcp_nopush on; #鏈接超時時間 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #開啓gzip壓縮 gzip on; gzip_disable "MSIE [1-6]."; #設定請求緩衝 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #設定虛擬主機配置 server { #偵聽80端口 listen 80; #定義使用 www.nginx.cn訪問 server_name www.nginx.cn; #定義服務器的默認網站根目錄位置 root html; #設定本虛擬主機的訪問日誌 access_log logs/nginx.access.log main; #默認請求 location / { #定義首頁索引文件的名稱 index index.php index.html index.htm; } # 定義錯誤提示頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { } #靜態文件,nginx本身處理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { #過時30天,靜態文件不怎麼更新,過時能夠設大一點, #若是頻繁更新,則能夠設置得小一點。 expires 30d; } #PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置. location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #禁止訪問 .htxxx 文件 location ~ /.ht { deny all; } } }
配置80端口
# Virtual Host configuration for arlingbc.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # # 丟棄缺少 Host 頭的請求 server { listen 80; return 444; } server { listen 80; listen [::]:80; server_name example.com www.example.com; # 定義服務器的默認網站根目錄位置 root /var/www/example/; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; # access log file 訪問日誌 access_log logs/nginx.access.log main; # 禁止訪問隱藏文件 # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } # 默認請求 location / { # 首先嚐試將請求做爲文件提供,而後做爲目錄,而後回退到顯示 404。 # try_files 指令將會按照給定它的參數列出順序進行嘗試,第一個被匹配的將會被使用。 # try_files $uri $uri/ =404; try_files $uri $uri/ /index.php?path_info=$uri&$args =404; access_log off; expires max; } # 靜態文件,nginx 本身處理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { #過時 30 天,靜態文件不怎麼更新,過時能夠設大一點, #若是頻繁更新,則能夠設置得小一點。 expires 30d; } # .php 請求 location ~ \.php$ { try_files $uri =404; include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; } # PHP 腳本請求所有轉發到 FastCGI 處理. 使用 FastCGI 默認配置. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: # fastcgi_pass unix:/run/php/php7.0-fpm.sock; #} # 拒絕訪問. htaccess 文件,若是 Apache 的文檔根與 nginx 的一致 # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
配置https 443端口
## # 80 port ## # 默認服務器,丟棄缺少 Host 頭的請求 server { listen 80; return 444; } server { listen 80; listen [::]:80; sever_name example.com www.example.com; rewrite ^(.*)$ https://$host$1 permanent; ## 端口轉發 } ## # 443 port ## server { ## # 阿里雲參考配置 ## listen 443; listen [::]:443; server_name example.com www.example.com; root /var/www/example/; # 爲虛擬服務器指明文檔的根目錄 index index.html index.htm; # 給定URL文件 ## # 部署 HTTP 嚴格傳輸安全(HSTS) ## add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"; # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 gzip off; ## # SSL configuration ## ssl on; ssl_certificate cert/certfile.pem; # 證書 ssl_certificate_key cert/certfile.key; # 私鑰 ssl_session_timeout 5m; # 設置超時時間 # 密碼套件配置 # 密碼套件名稱構成:密鑰交換-身份驗證-加密算法(算法-強度-模式)-MAC或PRF ssl_ciphers ECDHE-RSA-AES128-GCM- SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1.2; # 設置 SSL/TSL 協議版本號 ssl_prefer_server_ciphers on; # 控制密碼套件優先級,讓服務器選擇要使用的算法套件 ssl_buffer_size 1400; # 減小TLS緩衝區大小,能夠顯著減小首字節時間(《HTTPS權威指南》P416) ## # location configuration ## # ... }
http { # 最大鏈接數 # 分配一個共享內存區域,大小爲 10M,用於限制IP # 使用 $binary_remote_addr 變量, 能夠將每條狀態記錄的大小減小到 64 個字節,這樣 1M 的內存能夠保存大約 1 萬 6 千個 64 字節的記錄。 limit_conn_zone $binary_remote_addr zone=ips:10m; # 分配一個共享內存區域,大小爲 10M,用於限制服務器鏈接數 limit_conn_zone $server_name zone=servers:10m; # 設置日誌記錄級別 # 當服務器由於頻率太高拒絕或者延遲處理請求時能夠記下相應級別的日誌。 limit_conn_log_level notice; server { # 限制每個IP地址訪問限制10個鏈接 limit_conn ips 10; # 服務器提供的最大鏈接數 1000 limit_conn servers 1000; } }
http { # 最大鏈接數 # 分配一個共享內存區域,大小爲 10M,限制下載鏈接數爲1 limit_conn_zone $binary_remote_addr zone=connections:10m; # 最大併發數,每秒請求數(r/s),每分鐘請求數(r/m) # 分配一個設置最大併發數的內存區域,大小 10M,limit_req 限制以前的請求速率 1次/s。 # 使用 $binary_remote_addr 變量, 能夠將每條狀態記錄的大小減小到 64 個字節,這樣 1M 的內存能夠保存大約 1 萬 6 千個 64 字節的記錄。 limit_req_zone $binary_remote_addr zone=requests:10m rate=1r/s; # 設置日誌記錄級別 # 當服務器由於頻率太高拒絕或者延遲處理請求時能夠記下相應級別的日誌。 limit_req_log_level warn; # immediately release socket buffer memory on timeout reset_timedout_connection on; server { # 僅對 search URL 有效 location /search { # 限制速率 # 最大延遲請求數量 10 個,超過則返回狀態碼 503 limit_req zone=requests burst=3 nodelay; } # 限制客戶端帶寬, # 策略:容許小文件自由下載,但對於大文件則啓用這種限制 location /downloads { # 首先限制客戶端的下載鏈接數爲1 limit_conn connections 1; # 下載完 1M 內容以後,啓用 limit_rate 限制。 limit_rate_after 1m; # 限制客戶端下載下載內容的速率爲 500k/s limit_rate 500k; } } }
Nginx相關地址
源碼:https://trac.nginx.org/nginx/browser
官網:http://www.nginx.org/
nginx從入門到精通:http://tengine.taobao.org/book/index.html
中文文檔:http://www.nginx.cn/doc/
將全部的http請求經過rewrite重寫到https上便可
server { listen 192.168.1.111:80; server_name test.com; rewrite ^(.*)$ https://$host$1 permanent;
解釋:當此虛擬站點只容許https訪問時,當用http訪問時nginx會報出497錯誤碼
利用error_page命令將497狀態碼的連接重定向到https://test.com這個域名上
server { listen 192.168.1.11:443; #ssl端口 listen 192.168.1.11:80; #用戶習慣用http訪問,加上80,後面經過497狀態碼讓它自動跳到443端口 server_name test.com; #爲一個server{......}開啓ssl支持 ssl on; #指定PEM格式的證書文件 ssl_certificate /etc/nginx/test.pem; #指定PEM格式的私鑰文件 ssl_certificate_key /etc/nginx/test.key; #讓http請求重定向到https請求 error_page 497 https://$host$uri?$args;
<html> <meta http-equiv="refresh" content="0;url=https://test.com/"> </html>
server { listen 192.168.1.11:80; server_name test.com; location / { #index.html放在虛擬主機監聽的根目錄下 root /srv/www/http.test.com/; } #將404的頁面重定向到https的首頁 error_page 404 https://test.com/;