源引:http://www.cnblogs.com/knowledgesea/p/5175711.html
Nginx常用功能
一抖仅、代理:尤其是反向代理。
二、負(fù)載均衡
負(fù)載均衡策略有2種:
- 內(nèi)置策略
- 擴(kuò)展策略
1渣刷、內(nèi)置策略
1)輪詢 峰档、加權(quán)輪詢床三。
2) IP hash算法
對(duì)客戶端請(qǐng)求的ip進(jìn)行hash操作踱卵,然后根據(jù)hash結(jié)果將同一個(gè)客戶端ip的請(qǐng)求分發(fā)給同一臺(tái)服務(wù)器進(jìn)行處理惠毁,可以解決session不共享
的問(wèn)題槐瑞。
2熙涤、擴(kuò)展策略
你可以參照所有的負(fù)載均衡算法,給他一一找出來(lái)并做實(shí)現(xiàn)困檩。
3祠挫、web緩存
Nginx可以對(duì)不同的文件做不同的緩存處理,配置靈活悼沿,并且支持FastCGI_Cache
等舔,主要用于對(duì)FastCGI
的動(dòng)態(tài)程序進(jìn)行緩存。配合著第三方的ngx_cache_purge
糟趾,對(duì)制定的URL緩存內(nèi)容可以的進(jìn)行增刪管理慌植。
4、Nginx相關(guān)地址
源碼:https://trac.nginx.org/nginx/browser
官網(wǎng):http://www.nginx.org/
三义郑、Nginx配置文件結(jié)構(gòu)
打開(kāi)conf文件夾的nginx.conf
文件蝶柿,Nginx服務(wù)器的基礎(chǔ)配置,默認(rèn)的配置也存放在此非驮。
1交汤、默認(rèn)配置
#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文件結(jié)構(gòu)
#全局塊
...
#events塊
events {
...
}
#http塊
http {
#http全局塊
...
#server塊
server {
#server全局塊
......
#location塊
location [PATTERN] {
......
}
location [PATTERN] {
...
}
}
server {
...
}
#http全局塊
...
}
1)劫笙、全局塊:
配置影響nginx全局的指令芙扎。一般有:
- 運(yùn)行nginx服務(wù)器的用戶組星岗;
- nginx進(jìn)程pid存放路徑;
- 日志存放路徑纵顾;
- 配置文件引入伍茄;
- 允許生成worker process數(shù)等
2)、events塊:
配置影響nginx服務(wù)器或與用戶的網(wǎng)絡(luò)連接施逾。
- 每個(gè)進(jìn)程的最大連接數(shù)
- 選取哪種事件驅(qū)動(dòng)模型處理連接請(qǐng)求
- 是否允許同時(shí)接受多個(gè)網(wǎng)路連接
- 開(kāi)啟多個(gè)網(wǎng)絡(luò)連接序列化等敷矫。
3、http塊:
可以嵌套多個(gè)server汉额,配置代理曹仗,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置蠕搜。
如文件引入怎茫,mime-type定義,日志自定義妓灌,是否使用sendfile傳輸文件轨蛤,連接超時(shí)時(shí)間,單連接請(qǐng)求數(shù)等虫埂。
4祥山、server塊:
配置虛擬主機(jī)的相關(guān)參數(shù),一個(gè)http中可以有多個(gè)server掉伏。
5缝呕、location塊:
配置請(qǐng)求的路由,以及各種頁(yè)面的處理情況斧散。
########### 每個(gè)指令必須有分號(hào)結(jié)束供常。##################
user administrator administrators; #配置用戶或者組,默認(rèn)為nobody nobody鸡捐。
#worker_processes 2; #允許生成的進(jìn)程數(shù)栈暇,默認(rèn)為1
#pid /nginx/pid/nginx.pid; #指定nginx進(jìn)程運(yùn)行文件存放地址
error_log log/error.log debug; #指定日志路徑,級(jí)別箍镜。這個(gè)設(shè)置可以放入全局塊瞻鹏,http塊,server塊鹿寨,級(jí)別以此為:
#debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生薪夕,默認(rèn)為on
multi_accept on; #設(shè)置一個(gè)進(jìn)程是否同時(shí)接受多個(gè)網(wǎng)絡(luò)連接脚草,默認(rèn)為off
#use epoll; #事件驅(qū)動(dòng)模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大連接數(shù)原献,默認(rèn)為512
}
http {
include mime.types; #文件擴(kuò)展名與文件類型映射表
default_type application/octet-stream; #默認(rèn)文件類型馏慨,默認(rèn)為text/plain
# 配置跨域問(wèn)題
add_header Access-Control-Allow-Origin *; ##全匹配方式埂淮。
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
#access_log off; #取消服務(wù)日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
#自定義log格式
access_log log/access.log myFormat; #combined 為日志格式的默認(rèn)值
sendfile on; #允許sendfile方式傳輸文件,默認(rèn)為off写隶,可以在http塊倔撞,server塊,location塊慕趴。
sendfile_max_chunk 100k; #每個(gè)進(jìn)程每次調(diào)用傳輸數(shù)量不能大于設(shè)定的值痪蝇,默認(rèn)為0,即不設(shè)上限冕房。
server_names_hash_bucket_size 64; #hash表可保存服務(wù)器名字的數(shù)量(32的倍數(shù))
keepalive_timeout 65; #連接超時(shí)時(shí)間躏啰,默認(rèn)為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; #錯(cuò)誤頁(yè)
server {
keepalive_requests 120; #單連接請(qǐng)求上限次數(shù)详拙。
listen 4545; #監(jiān)聽(tīng)端口
server_name 127.0.0.1; #監(jiān)聽(tīng)地址帝际,不需要加http://
location ~*^.+$ { #請(qǐng)求的url過(guò)濾,正則匹配饶辙,~為區(qū)分大小寫(xiě)蹲诀,~*為不區(qū)分大小寫(xiě)。
#root path; #根目錄
#index vv.txt; #設(shè)置默認(rèn)頁(yè)
proxy_pass http://mysvr; #請(qǐng)求轉(zhuǎn)向mysvr 定義的服務(wù)器列表
deny 127.0.0.1; #拒絕的ip
allow 172.18.5.54; #允許的ip
}
}
}
上面是nginx的基本配置畸悬,需要注意的有以下幾點(diǎn):
1侧甫、
1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
2.$remote_user :用來(lái)記錄客戶端用戶名稱蹋宦;
3.$time_local : 用來(lái)記錄訪問(wèn)時(shí)間與時(shí)區(qū)披粟;
4.$request : 用來(lái)記錄請(qǐng)求的url與http協(xié)議;
5.$status : 用來(lái)記錄請(qǐng)求狀態(tài)冷冗;成功是200守屉,
6.$body_bytes_sent :記錄發(fā)送給客戶端文件主體內(nèi)容大小蒿辙;
7.$http_referer :用來(lái)記錄從那個(gè)頁(yè)面鏈接訪問(wèn)過(guò)來(lái)的拇泛;
8.$http_user_agent :記錄客戶端瀏覽器的相關(guān)信息;
2思灌、驚群現(xiàn)象:
一個(gè)網(wǎng)路連接到來(lái)俺叭,多個(gè)睡眠的進(jìn)程被同時(shí)叫醒,但只有一個(gè)進(jìn)程能獲得鏈接泰偿,這樣會(huì)影響系統(tǒng)性能熄守。
3、每個(gè)指令必須有分號(hào)結(jié)束
4、啟動(dòng)與停止
#啟動(dòng)
$ sudo /etc/init.d/nginx
或
$ nginx (如果你配置了環(huán)境變量)
# 重啟
$ nginx -s reload
#停止
$ sudo service nginx stop
或
$ ps -e | grep nginx #查看進(jìn)程ID裕照,然后
$ kill 進(jìn)程ID
四攒发、常用指令
- autoindex on; // 指定該目錄下的所有文件可通過(guò)瀏覽器直接訪問(wèn)
示例:
location /images/ {
root /var/www/images/;
autoindex on;
}
五、安全策略配置
server {
###
# 瀏覽器默認(rèn)會(huì)對(duì)靜態(tài)資源進(jìn)行Content-Type 的猜測(cè)
# (如果沒(méi)設(shè)置晋南,或者故意設(shè)置錯(cuò)惠猿,比如一個(gè)js文件,缺被黑客設(shè)置成為image/png)负间,
# 設(shè)置 X-Content-Type-Options "nosniff" 偶妖,禁止瀏覽器進(jìn)行猜測(cè)。
##
add_header X-Content-Type-Options "nosniff";
##
# 如果發(fā)現(xiàn)有跨站攻擊唉擂,則立刻停止頁(yè)面服務(wù)
##
add_header X-XSS-Protection "1; mode=block";
##
# 我們的網(wǎng)站發(fā)送請(qǐng)求的時(shí)候餐屎,往往在 請(qǐng)求頭中帶上 Referer,
# 我么可以通過(guò)這個(gè)來(lái)控制玩祟。 可選值自行查詢
##
add_header Referrer-Policy "no-referrer-when-downgrade";
add_header X-Permitted-Cross-Domain-Policies 'none';
add_header X-Download-Options 'noopen';
##
# 用于控制網(wǎng)站可以在哪些被允許的域名網(wǎng)站中以 <iframe>的形式嵌入
# DENY :不允許被任何頁(yè)面嵌入腹缩;
# SAMEORIGIN :不允許被本域以外的頁(yè)面嵌入;
# ALLOW-FROM uri :不允許被指定的域名以外的頁(yè)面嵌入(Chrome現(xiàn)階段不支持)空扎;
##
add_header X-Frame-Options "SAMEORIGIN";
##
# 控制當(dāng)前網(wǎng)站只可以加載 哪里的資源
# default-src * 表示除了單獨(dú)指定的類型資源以外藏鹊,其他資源都按照這個(gè)設(shè)置來(lái)
# script-src https://*.facebook.com http://*.facebook.com 表示 javascript 腳本的資源只能從 https://*.facebook.com http://*.facebook.com 這兩個(gè)地方加載
# style-src https://*.facebook.com http://*.facebook.com 表示 css樣式表 的資源只能從 https://*.facebook.com http://*.facebook.com 這兩個(gè)地方加載
# 還有很多,請(qǐng)自定查詢
##
add_header Content-Security-Policy default-src *;script-src https://*.facebook.com http://*.facebook.com;style-src https://*.facebook.com;
# strict-transport-security "max-age=31536000;" # 在接下來(lái)的365天內(nèi)強(qiáng)制使用https转锈,需檢查自己服務(wù)器是否支持https
add_header strict-transport-security "max-age=31536000;";
}