新聞中心
眾所周知,Nginx?是 Apache服務(wù)不錯(cuò)的替代品。其特點(diǎn)是占有內(nèi)存少,并發(fā)能力強(qiáng),事實(shí)上?Nginx?的并發(fā)能力在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好,因此國內(nèi)知名大廠例如:淘寶,京東,百度,新浪,網(wǎng)易,騰訊等等都在使用Nginx網(wǎng)站。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供方城網(wǎng)站建設(shè)、方城做網(wǎng)站、方城網(wǎng)站設(shè)計(jì)、方城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、方城企業(yè)網(wǎng)站模板建站服務(wù),十載方城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
Nginx簡介
Nginx?是開源、高性能、高可靠的 Web 和反向代理服務(wù)器,而且支持熱部署,同時(shí)也提供了 IMAP/POP3/SMTP 服務(wù),可以不間斷運(yùn)行,提供熱更新功能。占用內(nèi)存少、并發(fā)能力強(qiáng),最重要的是,Nginx?是免費(fèi)的并可以商業(yè)化,配置使用都比較簡單。
Nginx 特點(diǎn)
-
高并發(fā)、高性能 -
模塊化架構(gòu)使得它的擴(kuò)展性非常好 -
異步非阻塞的事件驅(qū)動(dòng)模型這點(diǎn)和 Node.js 相似 -
無需重啟可不間斷運(yùn)行 -
熱部署、平滑升級 -
完全開源,生態(tài)好
Nginx 最重要的幾個(gè)使用場景:
-
靜態(tài)資源服務(wù) -
反向代理服務(wù),包括緩存、負(fù)載均衡等 -
API 服務(wù),OpenResty
所以,今天民工哥就給大家整理一份?Nginx的常用配置清單,供大家學(xué)習(xí)與生產(chǎn)配置參考使用。主要包括以下三個(gè)方面:
-
基礎(chǔ)配置 -
高級配置 -
安全配置
基礎(chǔ)配置
去掉不用的 Nginx 模塊
./configure?--without-module1?--without-module2?--without-module3
例如:
./configure?--without-http_dav_module?--withouthttp_spdy_module
#注意事項(xiàng):配置指令是由模塊提供的。確保你禁用的模塊不包含你需要使用的指令!在決定禁用模塊之前,應(yīng)該檢查Nginx文檔中每個(gè)模塊可用的指令列表。
進(jìn)程相關(guān)的配置
worker_processes?8;
#Nginx 進(jìn)程數(shù),建議按照CPU數(shù)目來指定,一般為它的倍數(shù)?(如,2個(gè)四核的CPU計(jì)為8)。
worker_rlimit_nofile?65535;??
#一個(gè)Nginx?進(jìn)程打開的最多文件描述符數(shù)目
worker_connections?65535;
#每個(gè)進(jìn)程允許的最多連接數(shù)
監(jiān)聽端口
server?{
????????listen???????80;???#監(jiān)聽端口
????????server_name??www.mingongge.com;??#域名信息
????????location?/?{
????????????root???/www/www;???#網(wǎng)站根目錄
????????????index??index.html?index.htm;??#默認(rèn)首頁類型
????????????deny?192.168.2.11;???#禁止訪問的ip地址,可以為all
??????????? allow 192.168.3.44;?#允許訪問的ip地址,可以為all
????????}
????????}???????????
小技巧補(bǔ)充:域名匹配的四種寫法
精確匹配:server_name www.mingongge.com ;
左側(cè)通配:server_name *.mingongge.com ;
右側(cè)統(tǒng)配:server_name www.mingongge.*?;
正則匹配:server_name ~^www\.mingongge\.*$?;
匹配優(yōu)先級:精確匹配?>?左側(cè)通配符匹配?>?右側(cè)通配符匹配?>?正則表達(dá)式匹配
配置 Nginx 狀態(tài)頁面
[root@proxy?~]#?cat?/usr/local/nginx/conf/nginx.conf
…?…
location?/NginxStatus?{
??????stub_status???????????on;
??????access_log????????????on;
??????auth_basic????????????"NginxStatus";
??????auth_basic_user_file??conf/htpasswd;
????????}
…?…
[root@proxy?~]#?/usr/local/nginx/sbin/nginx?-s?reload
Nginx 日志(訪問與錯(cuò)誤日志管理)
error_log??/var/log/nginx/error.log?warn;
#配置錯(cuò)誤日志的級別及存儲(chǔ)目錄
events?{
????worker_connections??1024;
}
http?{
..................
????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??/var/log/nginx/access.log?main;
????#配置訪問日志存儲(chǔ)目錄
}
以上配置只是Nginx自身關(guān)于日志的基本配置,在實(shí)際生產(chǎn)環(huán)境中,我們需要收集日志、分析日志,才定更好的去定位問題,
http 相關(guān)的配置
http?{
????sendfile??on??????????????????#高效傳輸文件的模式?一定要開啟
????keepalive_timeout???65????????#客戶端服務(wù)端請求超時(shí)時(shí)間
?
}
靜態(tài)資源配置
server?{??
listen?80;??
server_name?mingongge.com;??
location?/static?{??????
??root?/wwww/web/web_static_site;?
??}
}
也可以使用下面的方法
location?/image?{
?alias?/web/nginx/static/image/;
}
注意:使用alias末尾一定要添加/,并且它只能位于location中
反向代理
比如生產(chǎn)環(huán)境(同一臺(tái)服務(wù)中)有不同的項(xiàng)目,這個(gè)就比較實(shí)用了,用反向代理去做請示轉(zhuǎn)發(fā)。
http?{
.............
????upstream?product_server{
????????127.0.0.1:8081;
????}
????upstream?admin_server{
????????127.0.0.1:8082;
????}
????upstream?test_server{
????????127.0.0.1:8083;
????}
server?{
??????
??#默認(rèn)指向product的server
??location?/?{
??????proxy_pass?http://product_server;
??????}
??location?/product/{
??????proxy_pass?http://product_server;
?????}
??location?/admin/?{
??????proxy_pass?http://admin_server;
?????}
??location?/test/?{
??????proxy_pass?http://test_server;
??????}
????}
}
負(fù)載均衡
upstream?server_pools?{?
??server?192.168.1.11:8880???weight=5;
??server?192.168.1.12:9990???weight=1;
??server?192.168.1.13:8989???weight=6;
??#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大
}
server?{??
??listen?80;?
??server_name?mingongge.com;
??location?/?{????
??proxy_pass?http://server_pools;?
???}
}
代理相關(guān)的其它配置
proxy_connect_timeout?90;??#nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí))
proxy_send_timeout?90;?????#后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí))
proxy_read_timeout?90;?????#連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí))
proxy_buffer_size?4k;??????#代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffers?4?32k;??????#proxy_buffers緩沖區(qū)
proxy_busy_buffers_size?64k;?????#高負(fù)荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size?64k;??#設(shè)定緩存文件夾大小
proxy_set_header?Host?$host;?
proxy_set_header?X-Forwarder-For?$remote_addr;??#獲取客戶端真實(shí)IP
高級配置
重定向配置
location?/?{
?return?404;?#直接返回狀態(tài)碼
}
location?/?{
?return?404?"pages?not?found";?#返回狀態(tài)碼?+?一段文本
}
location?/?{
?return?302?/blog?;?#返回狀態(tài)碼?+?重定向地址
}
location?/?{
?return?https://www.mingongge.com?;?#返回重定向地址
}
示例如下
server?{?
listen?80;
server_name?www.mingongge.com;
return?301?http://mingongge.com$request_uri;
}
server?{
listen?80;?
server_name?www.mingongge.com;?
location?/cn-url?{?
???return?301?http://mingongge.com.cn;?
???}
}
server{
??listen?80;
??server_name?mingongge.com;?#?要在本地hosts文件進(jìn)行配置
??root?html;
??location?/search?{
???rewrite?^/(.*)?https://www.mingongge.com?redirect;
??}
??
??location?/images?{
???rewrite?/images/(.*)?/pics/$1;
??}
??
??location?/pics?{
???rewrite?/pics/(.*)?/photos/$1;
??}
??
??location?/photos?{
??
??}
}
設(shè)置緩沖區(qū)容量上限
這樣的設(shè)置可以阻止緩沖區(qū)溢出攻擊(同樣是Server模塊)
client_body_buffer_size?1k;
client_header_buffer_size?1k;
client_max_body_size?1k;
large_client_header_buffers?2?1k;
#設(shè)置后,不管多少HTTP請求都不會(huì)使服務(wù)器系統(tǒng)的緩沖區(qū)溢出了
限制最大連接數(shù)
在http模塊內(nèi)server模塊外配置limit_conn_zone,配置連接的IP,在http,server或location模塊配置limit_conn,能配置IP的最大連接數(shù)。
limit_conn_zone?$binary_remote_addr?zone=addr:5m;
limit_conn?addr?1;
Gzip壓縮
gzip_types??
#壓縮的文件類型
?text/plain?text/css?
?application/json?
?application/x-javascript?
?text/xml?application/xml?
?application/xml+rss?
?text/javascript
gzip?on;
#采用gzip壓縮的形式發(fā)送數(shù)據(jù)
gzip_disable?"msie6"
#為指定的客戶端禁用gzip功能
gzip_static;
#壓縮前查找是否有預(yù)先gzip處理過的資源
gzip_proxied?any;
#允許或者禁止壓縮基于請求和響應(yīng)的響應(yīng)流
gzip_min_length??1000;
#設(shè)置對數(shù)據(jù)啟用壓縮的最少字節(jié)數(shù)
gzip_comp_level?6;
#設(shè)置數(shù)據(jù)的壓縮等級
緩存配置
open_file_cache
#指定緩存最大數(shù)目以及緩存的時(shí)間
open_file_cache_valid
#在open_file_cache中指定檢測正確信息的間隔時(shí)間
open_file_cache_min_uses???
#定義了open_file_cache中指令參數(shù)不活動(dòng)時(shí)間期間里最小的文件數(shù)
open_file_cache_errors?????
#指定了當(dāng)搜索一個(gè)文件時(shí)是否緩存錯(cuò)誤信息
location?~?.*\.(gif|jpg|jpeg|png|bmp|swf)$
#指定緩存文件的類型
????????{
????????expires?3650d;????
???????????#指定緩存時(shí)間
????????}
????????location?~?.*\.(js|css)?$
????????{
????????expires?3d;?????????????????????
????????}
SSL 證書配及跳轉(zhuǎn)HTTPS配置
server?{
??????listen?192.168.1.250:443?ssl;
??????server_tokens?off;
??????server_name?mingonggex.com?www.mingonggex.com;
??????root?/var/www/mingonggex.com/public_html;
??????ssl_certificate?/etc/nginx/sites-enabled/certs/mingongge.crt;
??????ssl_certificate_key?/etc/nginx/sites-enabled/certs/mingongge.key;
??????ssl_protocols?TLSv1?TLSv1.1?TLSv1.2;
}
#?Permanent?Redirect?for?HTTP?to?HTTPS
server?
{??
listen?80;??
server_name?mingongge.com;?
https://$server_name$request_uri;
}
流量鏡像功能
location?/?{
????mirror?/mirror;
????proxy_pass?http://backend;
}
location?=?/mirror?{
????internal;
????proxy_pass?http://test_backend$request_uri;
}
限流功能
流量限制配置兩個(gè)主要的指令,limit_req_zone和limit_req
limit_req_zone?$binary_remote_addr?zone=mylimit:10m?rate=10r/s;
server?{
????location?/login/?{
????????limit_req?zone=mylimit;
????????proxy_pass?http://my_upstream;
????}
}
Nginx常用的內(nèi)置變量
安全配置
禁用server_tokens項(xiàng)
server_tokens在打開的情況下會(huì)使404頁面顯示Nginx的當(dāng)前版本號。這樣做顯然不安全,因?yàn)楹诳蜁?huì)利用此信息嘗試相應(yīng)Nginx版本的漏洞。只需要在nginx.conf中http模塊設(shè)置server_tokens off即可,例如:
server?{
????listen?192.168.1.250:80;
????Server_tokens?off;
????server_name?mingongge.com?www.mingongge.com;
????access_log?/var/www/logs/mingongge.access.log;
????error_log?/var/www/logs/mingonggex.error.log?error;
????root?/var/www/mingongge.com/public_html;
????index?index.html?index.htm;
}
#重啟Nginx后生效:
禁止非法的HTTP User Agents
User Agent是HTTP協(xié)議中對瀏覽器的一種標(biāo)識(shí),禁止非法的User Agent可以阻止爬蟲和掃描器的一些請求,防止這些請求大量消耗Nginx服務(wù)器資源。
為了更好的維護(hù),最好創(chuàng)建一個(gè)文件,包含不期望的user agent列表例如/etc/nginx/blockuseragents.rules包含如下內(nèi)容:
map?$http_user_agent?$blockedagent?{
????default?0;
????~*malicious?1;
????~*bot?1;
????~*backdoor?1;
????~*crawler?1;
????~*bandit?1;
}
然后將如下語句放入配置文件的server模塊內(nèi)
include?/etc/nginx/blockuseragents.rules;
并加入if語句設(shè)置阻止后進(jìn)入的頁面:
阻止圖片外鏈
location?/img/?{
??????valid_referers?none?blocked?192.168.1.250;
????????if?($invalid_referer)?{
??????????return?403;
??????}
}
禁掉不需要的 HTTP 方法
一些web站點(diǎn)和應(yīng)用,可以只支持GET、POST和HEAD方法。在配置文件中的 serve r模塊加入如下方法可以阻止一些欺騙攻擊
if?($request_method?!~?^(GET|HEAD|POST)$)?{
????return?444;
}
禁止 SSL 并且只打開 TLS
盡量避免使用SSL,要用TLS替代,以下配置可以放在Server模塊內(nèi)
ssl_protocols?TLSv1?TLSv1.1?TLSv1.2;
通過這一系列的配置之后,相信你的Nginx服務(wù)器足夠應(yīng)付實(shí)際生產(chǎn)需求了。
分享名稱:Nginx常用配置匯總!從入門到干活足矣
文章路徑:http://m.fisionsoft.com.cn/article/coihgsg.html


咨詢
建站咨詢
