本文已同步到專業技術網站 www.sufaith.com, 該網站專一於先後端開發技術與經驗分享, 包含Web開發、Nodejs、Python、Linux、IT資訊等板塊.javascript
Nginx安裝簡單、配置簡潔、啓動快速便捷、支持熱部署、支持 SSL、擁有高度模塊化的設計。
php
Nginx的主要功能有:
css
/usr/local/nginx/sbin/nginx複製代碼
/usr/local/nginx/sbin/nginx -s reopen複製代碼
/usr/local/nginx/sbin/nginx -s reload複製代碼
/usr/local/nginx/sbin/nginx -s stop複製代碼
(1) 查看進程號html
ps -ef|grep nginx複製代碼
kill -QUIT <進程號> 或 kill -TERM <進程號>複製代碼
pkill -9 nginx複製代碼
每一個server虛擬主機都定義了 location 指令,location 定義了對於指定的一組 URI 是如何匹配和進行處理的。vue
server {
listen 80;
server_name www.example.com;
location / {
root /usr/local/www;
index index.html;
}
}複製代碼
server {
location 表達式 {
}
}複製代碼
(2) location表達式的類型
java
(3) location表達式的優先級
nginx
(1) 語法
web
server {
rewrite 規則 定向路徑 重寫類型;
}複製代碼
(2) 示例
正則表達式
server {
listen 80;
server_name www.aaa.com;
location / {
rewrite ^/$ www.bbb.com permanent ;
}
}複製代碼
(1) 語法算法
try_files file1 files2 ... uri複製代碼
try_files $uri $uri/ /index.php?q=$uri&$args;複製代碼
(2) 示例
當訪問:www.example.com/test 時會依次查找,若 1.html,2.html 都不存在,最終返回 3.html
server {
listen 80;
server_name www.example.com;
root html;
index index.html;
location /test {
try_files /1.html /2.html /3.html;
}
}複製代碼
當訪問:www.example.com/test 時會依次查找,若 1.html,2.html 都不存在,則跳轉到命名爲abc的location
server {
listen 80;
server_name www.example.com;
root html;
index index.html;
location /test {
try_files /1.html /2.html @abc;
}
location @abc{
rewrite ^/(.*)$ http://www.example2.com;
}
}複製代碼
location / {
# URL 匹配不到任何靜態資源,返回同一個 index.html 頁面,這個頁面就是你 app 依賴的頁面。
try_files $uri $uri/ /index.html;
}複製代碼
server {
# 開啓gzip 壓縮
gzip on;
# 設置gzip所需的http協議最低版本 (HTTP/1.1, HTTP/1.0)
gzip_http_version 1.1;
# 設置壓縮級別(1-9), 值越大壓縮率越高,同時消耗cpu資源也越多,建議設置在4左右
gzip_comp_level 4;
# 設置壓縮的最小字節數, 頁面Content-Length獲取
gzip_min_length 1000;
# 設置壓縮文件的類型 (text/html), 不建議壓縮圖片(如jpg、png自己已壓縮)
gzip_types text/plain application/javascript text/css;
#配置禁用gzip條件,支持正則。此處表示ie6及如下不啓用gzip(由於ie低版本不支持)
gzip_disable "MSIE [1-6]\.";
}複製代碼
http {
# 配置共享會話緩存大小,視站點訪問狀況設定
ssl_session_cache shared:SSL:10m;
# 配置會話超時時間
ssl_session_timeout 10m;
server {
listen 443;
server_name www.example.com;
ssl on;
# 設置長鏈接
keepalive_timeout 70;
# HSTS策略
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# 證書文件
ssl_certificate www.example.com.crt;
# 私鑰文件
ssl_certificate_key www.example.com.key;
# 優先採起服務器算法
ssl_prefer_server_ciphers on;
# 指定SSL協議
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 定義算法
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";
# 減小點擊劫持
add_header X-Frame-Options DENY;
# 禁止服務器自動解析資源類型
add_header X-Content-Type-Options nosniff;
# 防XSS攻擊
add_header X-Xss-Protection 1;
}
}複製代碼
server {
listen 80;
server_name www.example.com;
root html;
index index.html;
location /test {
# 請求host
proxy_set_header Host $http_host;
# 請求ip
proxy_set_header X-Real-IP $remote_addr;
# 請求協議
proxy_set_header X-Scheme $scheme;
# 代理服務器
proxy_pass http://localhost:3000;
}
}複製代碼
(1) upstream模塊
一個最基本的upstream模塊以下:
#動態服務器組, server是後端服務器,my_server是自定義的服務器組名稱。
upstream my_server {
server localhost:8001;
server localhost:8002;
server localhost:8003;
}複製代碼
在upstream模塊配置完成後,要讓指定的訪問反向代理到服務器組。
server {
listen 80;
server_name www.example.com;
root html;
index index.html;
location / {
# 反向代理到定義好的服務器組my_server
proxy_pass my_server;
}
}複製代碼
http {
upstream my_server {
server localhost:8001;
server localhost:8002;
server localhost:8003;
}
server {
listen 80;
server_name www.example.com;
root html;
index index.html;
location / {
# 反向代理到定義好的服務器組my_server
proxy_pass my_server;
}
}
}複製代碼
(1) 輪詢(默認方式)
表示每一個請求按時間順序逐一分配到不一樣的後端服務器。
upstream my_server {
server localhost:8001;
server localhost:8002;
}複製代碼
表示在輪詢策略的基礎上指定輪詢的服務器的權重,默認爲1,權重越高分配到須要處理的請求越多。
upstream my_server {
server localhost:8001 weight=1;
server localhost:8002 weight=2;
}複製代碼
表示指定負載均衡器按照基於客戶端IP的分配方式,這個方法確保了相同的客戶端的請求一直髮送到相同的服務器,以保證session會話。這樣每一個訪客都固定訪問一個後端服務器,能夠解決session不能跨服務器的問題。
upstream my_server {
ip_hash;
server localhost:8001;
server localhost:8002;
}複製代碼
(4) least_conn
upstream my_server {
least_conn;
server localhost:8001;
server localhost:8002;
}複製代碼
表示當前的server暫時不參與負載均衡。
upstream my_server {
server localhost:8001 down;
server localhost:8002;
server localhost:8003;
}複製代碼
表示預留的備份機器。當其餘全部的非backup機器出現故障或者忙的時候,纔會請求backup機器,因 此這臺機器的壓力最輕。
upstream my_server {
server localhost:8001 backup;
server localhost:8002;
server localhost:8003;
}複製代碼