Nginx 在工作中已經(jīng)有好幾個(gè)環(huán)境在使用了,每次都是重新去網(wǎng)上找博客,各種編譯配置,今天自己也整理一份安裝文檔和 nginx.conf 配置選項(xiàng)的說(shuō)明,留作以后參考。
1. 安裝nginx
1.1 選擇穩(wěn)定版本
我們編譯安裝nginx來(lái)定制自己的模塊,機(jī)器CentOS 6.2 x86_64。首先安裝缺少的依賴包:
# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
這些軟件包如果yum上沒(méi)有的話可以下載源碼來(lái)編譯安裝,只是要注意編譯時(shí)默認(rèn)安裝的目錄,確保下面在安裝nginx時(shí)能夠找到這些動(dòng)態(tài)庫(kù)文件(ldconfig)。
下載穩(wěn)定版到/usr/local/src下解壓。
為了后續(xù)準(zhǔn)備我們另外下載2個(gè)插件模塊:nginx_upstream_check_module-0.3.0.tar.gz —— 檢查后端服務(wù)器的狀態(tài),nginx-goodies-nginx-sticky-module-ng-bd312d586752.tar.gz(建議在/usr/local/src下解壓后將目錄重命名為nginx-sticky-module-ng-1.2.5) —— 后端做負(fù)載均衡解決session sticky問(wèn)題(與upstream_check模塊結(jié)合使用需要另外打補(bǔ)丁,請(qǐng)參考nginx負(fù)載均衡配置實(shí)戰(zhàn))。
請(qǐng)注意插件與nginx的版本兼容問(wèn)題,一般插件越新越好,nginx不用追新,穩(wěn)定第一。nginx-1.4.7,nginx-sticky-module-1.1,nginx_upstream_check_module-0.2.0,這個(gè)搭配也沒(méi)問(wèn)題。sticky-1.1與nginx-1.6版本由于更新沒(méi)跟上編譯出錯(cuò)。(可以直接使用Tengine,默認(rèn)就包括了這些模塊)
[root@cachets nginx-1.6.3]# pwd/usr/local/src/nginx-1.6.3[root@cachets nginx-1.6.3]# ./configure --prefix=/usr/local/nginx-1.6 --with-pcre /> --with-http_stub_status_module --with-http_ssl_module /> --with-http_gzip_static_module --with-http_realip_module /> --add-module=../nginx_upstream_check_module-0.3.0[root@cachets nginx-1.6.3]# make && make install
1.2 常用編譯選項(xiàng)說(shuō)明
nginx大部分常用模塊,編譯時(shí)./configure --help以--without開(kāi)頭的都默認(rèn)安裝。
再提供一種編譯方案:
./configure /> --prefix=/usr /> --sbin-path=/usr/sbin/nginx /> --conf-path=/etc/nginx/nginx.conf /> --error-log-path=/var/log/nginx/error.log /> --http-log-path=/var/log/nginx/access.log /> --pid-path=/var/run/nginx/nginx.pid /> --lock-path=/var/lock/nginx.lock /> --user=nginx /> --group=nginx /> --with-http_ssl_module /> --with-http_stub_status_module /> --with-http_gzip_static_module /> --http-client-body-temp-path=/var/tmp/nginx/client/ /> --http-proxy-temp-path=/var/tmp/nginx/proxy/ /> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ /> --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi /> --with-pcre=../pcre-7.8> --with-zlib=../zlib-1.2.3
1.3 啟動(dòng)關(guān)閉nginx
## 檢查配置文件是否正確# /usr/local/nginx-1.6/sbin/nginx -t# ./sbin/nginx -V # 可以看到編譯選項(xiàng) ## 啟動(dòng)、關(guān)閉# ./sbin/nginx # 默認(rèn)配置文件 conf/nginx.conf,-c 指定# ./sbin/nginx -s stop或 pkill nginx ## 重啟,不會(huì)改變啟動(dòng)時(shí)指定的配置文件# ./sbin/nginx -s reload或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid`
當(dāng)然也可以將 nginx 作為系統(tǒng)服務(wù)管理,下載 nginx 到/etc/init.d/,修改里面的路徑然后賦予可執(zhí)行權(quán)限。
# service nginx {start|stop|status|restart|reload|configtest}
1.4 yum安裝
yum安裝rpm包會(huì)比編譯安裝簡(jiǎn)單很多,默認(rèn)會(huì)安裝許多模塊,但缺點(diǎn)是如果你想以后安裝第三方模塊那就沒(méi)辦法了。
# vi /etc/yum.repo.d/nginx.repo[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1
剩下的就yum install nginx搞定,也可以yum install nginx-1.6.3安裝指定版本(前提是你去packages里看到有對(duì)應(yīng)的版本,默認(rèn)是最新版穩(wěn)定版)。
2. nginx.conf配置文件
Nginx配置文件主要分成四部分:main(全局設(shè)置)、server(主機(jī)設(shè)置)、upstream(上游服務(wù)器設(shè)置,主要為反向代理、負(fù)載均衡相關(guān)配置)和 location(URL匹配特定位置后的設(shè)置),每部分包含若干個(gè)指令。main部分設(shè)置的指令將影響其它所有部分的設(shè)置;server部分的指令主要用于指定虛擬主機(jī)域名、IP和端口;upstream的指令用于設(shè)置一系列的后端服務(wù)器,設(shè)置反向代理及后端服務(wù)器的負(fù)載均衡;location部分用于匹配網(wǎng)頁(yè)位置(比如,根目錄“/”,“/images”,等等)。他們之間的關(guān)系式:server繼承main,location繼承server;upstream既不會(huì)繼承指令也不會(huì)被繼承。它有自己的特殊指令,不需要在其他地方的應(yīng)用。
當(dāng)前nginx支持的幾個(gè)指令上下文:
2.1 通用
下面的nginx.conf簡(jiǎn)單的實(shí)現(xiàn)nginx在前端做反向代理服務(wù)器的例子,處理js、png等靜態(tài)文件,jsp等動(dòng)態(tài)請(qǐng)求轉(zhuǎn)發(fā)到其它服務(wù)器tomcat:
user www www;worker_processes 2; error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info; pid logs/nginx.pid; events {use epoll;worker_connections 2048;} 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 65; # gzip壓縮功能設(shè)置gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 6;gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;gzip_vary on; # http_proxy 設(shè)置client_max_body_size 10m;client_body_buffer_size 128k;proxy_connect_timeout 75;proxy_send_timeout 75;proxy_read_timeout 75;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;proxy_temp_path /usr/local/nginx/proxy_temp 1 2; # 設(shè)定負(fù)載均衡后臺(tái)服務(wù)器列表upstream backend {#ip_hash;server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;} # 很重要的虛擬主機(jī)配置server {listen 80;server_name itoatest.example.com;root /apps/oaapp; charset utf-8;access_log logs/host.access.log main; #對(duì) / 所有做負(fù)載均衡+反向代理location / {root /apps/oaapp;index index.jsp index.html index.htm; proxy_pass http://backend;proxy_redirect off;# 后端的Web服務(wù)器可以通過(guò)X-Forwarded-For獲取用戶真實(shí)IPproxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } #靜態(tài)文件,nginx自己處理,不去backend請(qǐng)求tomcatlocation ~* /download/ {root /apps/oa/fs; }location ~ .*/.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)${root /apps/oaapp;expires 7d;}location /nginx_status {stub_status on;access_log off;allow 192.168.10.0/24;deny all;} location ~ ^/(WEB-INF)/ {deny all;}#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;}} ## 其它虛擬主機(jī),server 指令開(kāi)始}
2.2 常用指令說(shuō)明
2.2.1 main全局配置
nginx在運(yùn)行時(shí)與具體業(yè)務(wù)功能(比如http服務(wù)或者email服務(wù)代理)無(wú)關(guān)的一些參數(shù),比如工作進(jìn)程數(shù),運(yùn)行的身份等。
1、woker_processes 2
在配置文件的頂級(jí)main部分,worker角色的工作進(jìn)程的個(gè)數(shù),master進(jìn)程是接收并分配請(qǐng)求給worker處理。這個(gè)數(shù)值簡(jiǎn)單一點(diǎn)可以設(shè)置為cpu的核數(shù)grep ^processor /proc/cpuinfo | wc -l,也是 auto 值,如果開(kāi)啟了ssl和gzip更應(yīng)該設(shè)置成與邏輯CPU數(shù)量一樣甚至為2倍,可以減少I/O操作。如果nginx服務(wù)器還有其它服務(wù),可以考慮適當(dāng)減少。
2、worker_cpu_affinity
也是寫在main部分。在高并發(fā)情況下,通過(guò)設(shè)置cpu粘性來(lái)降低由于多CPU核切換造成的寄存器等現(xiàn)場(chǎng)重建帶來(lái)的性能損耗。如worker_cpu_affinity 0001 0010 0100 1000; (四核)。
3、worker_connections 2048
寫在events部分。每一個(gè)worker進(jìn)程能并發(fā)處理(發(fā)起)的最大連接數(shù)(包含與客戶端或后端被代理服務(wù)器間等所有連接數(shù))。nginx作為反向代理服務(wù)器,計(jì)算公式 最大連接數(shù) = worker_processes * worker_connections/4,所以這里客戶端最大連接數(shù)是1024,這個(gè)可以增到到8192都沒(méi)關(guān)系,看情況而定,但不能超過(guò)后面的worker_rlimit_nofile。當(dāng)nginx作為http服務(wù)器時(shí),計(jì)算公式里面是除以2。
4、worker_rlimit_nofile 10240
寫在main部分。默認(rèn)是沒(méi)有設(shè)置,可以限制為操作系統(tǒng)最大的限制65535。
5、use epoll
寫在events部分。在Linux操作系統(tǒng)下,nginx默認(rèn)使用epoll事件模型,得益于此,nginx在Linux操作系統(tǒng)下效率相當(dāng)高。同時(shí)Nginx在OpenBSD或FreeBSD操作系統(tǒng)上采用類似于epoll的高效事件模型kqueue。在操作系統(tǒng)不支持這些高效模型時(shí)才使用select。
2.2.2 http服務(wù)器
與提供http服務(wù)相關(guān)的一些配置參數(shù)。例如:是否使用keepalive啊,是否使用gzip進(jìn)行壓縮等。
模塊http_proxy:
這個(gè)模塊實(shí)現(xiàn)的是nginx作為反向代理服務(wù)器的功能,包括緩存功能(另見(jiàn)文章)
proxy_pass,proxy_redirect見(jiàn) location 部分。
模塊http_gzip:
2.2.3 server虛擬主機(jī)
http服務(wù)上支持若干虛擬主機(jī)。每個(gè)虛擬主機(jī)一個(gè)對(duì)應(yīng)的server配置項(xiàng),配置項(xiàng)里面包含該虛擬主機(jī)相關(guān)的配置。在提供mail服務(wù)的代理時(shí),也可以建立若干server。每個(gè)server通過(guò)監(jiān)聽(tīng)地址或端口來(lái)區(qū)分。
1、listen
監(jiān)聽(tīng)端口,默認(rèn)80,小于1024的要以root啟動(dòng)。可以為listen *:80、listen 127.0.0.1:80等形式。
2、server_name
服務(wù)器名,如localhost、www.example.com,可以通過(guò)正則匹配。
模塊http_stream
這個(gè)模塊通過(guò)一個(gè)簡(jiǎn)單的調(diào)度算法來(lái)實(shí)現(xiàn)客戶端IP到后端服務(wù)器的負(fù)載均衡,upstream后接負(fù)載均衡器的名字,后端realserver以 host:port options; 方式組織在 {} 中。如果后端被代理的只有一臺(tái),也可以直接寫在 proxy_pass 。
2.2.4 location
http服務(wù)中,某些特定的URL對(duì)應(yīng)的一系列配置項(xiàng)。
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
這四個(gè)暫且這樣設(shè),如果深究的話,每一個(gè)都涉及到很復(fù)雜的內(nèi)容,也將通過(guò)另一篇文章來(lái)解讀。
關(guān)于location匹配規(guī)則的寫法,可以說(shuō)尤為關(guān)鍵且基礎(chǔ)的,參考文章 nginx配置location總結(jié)及rewrite規(guī)則寫法;
2.3 其它
2.3.1 訪問(wèn)控制 allow/deny
Nginx 的訪問(wèn)控制模塊默認(rèn)就會(huì)安裝,而且寫法也非常簡(jiǎn)單,可以分別有多個(gè)allow,deny,允許或禁止某個(gè)ip或ip段訪問(wèn),依次滿足任何一個(gè)規(guī)則就停止往下匹配。如:
location /nginx-status {stub_status on;access_log off;# auth_basic "NginxStatus";# auth_basic_user_file /usr/local/nginx-1.6/htpasswd; allow 192.168.10.100;allow 172.29.73.0/24;deny all;}
我們也常用 httpd-devel 工具的 htpasswd 來(lái)為訪問(wèn)的路徑設(shè)置登錄密碼:
# htpasswd -c htpasswd adminNew passwd:Re-type new password:Adding password for user admin # htpasswd htpasswd admin //修改admin密碼# htpasswd htpasswd sean //多添加一個(gè)認(rèn)證用戶
這樣就生成了默認(rèn)使用CRYPT加密的密碼文件。打開(kāi)上面nginx-status的兩行注釋,重啟nginx生效。
2.3.2 列出目錄 autoindex
Nginx默認(rèn)是不允許列出整個(gè)目錄的。如需此功能,打開(kāi)nginx.conf文件,在location,server 或 http段中加入autoindex on;,另外兩個(gè)參數(shù)最好也加上去:
默認(rèn)為off,顯示的文件時(shí)間為GMT時(shí)間。改為on后,顯示的文件時(shí)間為文件的服務(wù)器時(shí)間
location /images {root /var/www/nginx-default/images;autoindex on;autoindex_exact_size off;autoindex_localtime on;}
新聞熱點(diǎn)
疑難解答
圖片精選