來源:https://www.cnblogs.com/bluestorm/p/4574688.htmljavascript
原文出處: Sean Chow(@SeanLoook) 歡迎分享原創到伯樂頭條css
Nginx 在工做中已經有好幾個環境在使用了,每次都是從新去網上找博客,各類編譯配置,今天本身也整理一份安裝文檔和 nginx.conf 配置選項的說明,留做之後參考。html
1. 安裝nginx前端
1.1 選擇穩定版本java
咱們編譯安裝nginx來定製本身的模塊,機器CentOS 6.2 x86_64。首先安裝缺乏的依賴包:nginx
1c++ |
# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-develgit |
這些軟件包若是yum上沒有的話能夠下載源碼來編譯安裝,只是要注意編譯時默認安裝的目錄,確保下面在安裝nginx時可以找到這些動態庫文件(ldconfig)。github
從 http://nginx.org/en/download.html 下載穩定版nginx-1.6.3.tar.gz到/usr/local/src下解壓。正則表達式
爲了後續準備咱們另外下載2個插件模塊:nginx_upstream_check_module-0.3.0.tar.gz —— 檢查後端服務器的狀態,nginx-goodies-nginx-sticky-module-ng-bd312d586752.tar.gz(建議在/usr/local/src下解壓後將目錄重命名爲nginx-sticky-module-ng-1.2.5) —— 後端作負載均衡解決session sticky問題(與upstream_check模塊結合使用須要另外打補丁,請參考nginx負載均衡配置實戰)。
請注意插件與nginx的版本兼容問題,通常插件越新越好,nginx不用追新,穩定第一。nginx-1.4.7,nginx-sticky-module-1.1,nginx_upstream_check_module-0.2.0,這個搭配也沒問題。sticky-1.1與nginx-1.6版本因爲更新沒跟上編譯出錯。(能夠直接使用Tengine,默認就包括了這些模塊)
1 2 3 4 5 6 7 8 |
[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 經常使用編譯選項說明
nginx大部分經常使用模塊,編譯時./configure --help以--without開頭的都默認安裝。
再提供一種編譯方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
./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 啓動關閉nginx
1 2 3 4 5 6 7 8 9 10 11 12 |
## 檢查配置文件是否正確 # /usr/local/nginx-1.6/sbin/nginx -t # ./sbin/nginx -V # 能夠看到編譯選項
## 啓動、關閉 # ./sbin/nginx # 默認配置文件 conf/nginx.conf,-c 指定 # ./sbin/nginx -s stop 或 pkill nginx
## 重啓,不會改變啓動時指定的配置文件 # ./sbin/nginx -s reload 或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid` |
固然也能夠將 nginx 做爲系統服務管理,下載 nginx 到/etc/init.d/,修改裏面的路徑而後賦予可執行權限。
1 |
# service nginx {start|stop|status|restart|reload|configtest} |
1.4 yum安裝
—— 2015-05-22更新
yum安裝rpm包會比編譯安裝簡單不少,默認會安裝許多模塊,但缺點是若是你想之後安裝第三方模塊那就沒辦法了。
1 2 3 4 5 6 |
# vi /etc/yum.repo.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 |
剩下的就yum install nginx搞定,也能夠yum install nginx-1.6.3安裝指定版本(前提是你去packages裏看到有對應的版本,默認是最新版穩定版)。
2. nginx.conf配置文件
Nginx配置文件主要分紅四部分:main(全局設置)、server(主機設置)、upstream(上游服務器設置,主要爲反向代理、負載均衡相關配置)和 location(URL匹配特定位置後的設置),每部分包含若干個指令。main部分設置的指令將影響其它全部部分的設置;server部分的指令主要用於指定虛擬主機域名、IP和端口;upstream的指令用於設置一系列的後端服務器,設置反向代理及後端服務器的負載均衡;location部分用於匹配網頁位置(好比,根目錄「/」,「/images」,等等)。他們之間的關係式:server繼承main,location繼承server;upstream既不會繼承指令也不會被繼承。它有本身的特殊指令,不須要在其餘地方的應用。
當前nginx支持的幾個指令上下文:
2.1 通用
下面的nginx.conf簡單的實現nginx在前端作反向代理服務器的例子,處理js、png等靜態文件,jsp等動態請求轉發到其它服務器tomcat:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
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壓縮功能設置 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 設置 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;
# 設定負載均衡後臺服務器列表 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 ; }
# 很重要的虛擬主機配置 server { listen 80; server_name itoatest.example.com; root /apps/oaapp;
charset utf-8; access_log logs/host.access.log main;
#對 / 全部作負載均衡+反向代理 location / { root /apps/oaapp; index index.jsp index.html index.htm;
proxy_pass http://backend; proxy_redirect off; # 後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP proxy_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;
}
#靜態文件,nginx本身處理,不去backend請求tomcat location ~* /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; } }
## 其它虛擬主機,server 指令開始 } |
2.2 經常使用指令說明
2.2.1 main全局配置
nginx在運行時與具體業務功能(好比http服務或者email服務代理)無關的一些參數,好比工做進程數,運行的身份等。
2.2.2 http服務器
與提供http服務相關的一些配置參數。例如:是否使用keepalive啊,是否使用gzip進行壓縮等。
模塊http_proxy:
這個模塊實現的是nginx做爲反向代理服務器的功能,包括緩存功能(另見文章)
proxy_pass,proxy_redirect見 location 部分。
模塊http_gzip:
2.2.3 server虛擬主機
http服務上支持若干虛擬主機。每一個虛擬主機一個對應的server配置項,配置項裏面包含該虛擬主機相關的配置。在提供mail服務的代理時,也能夠創建若干server。每一個server經過監聽地址或端口來區分。
模塊http_stream
這個模塊經過一個簡單的調度算法來實現客戶端IP到後端服務器的負載均衡,upstream後接負載均衡器的名字,後端realserver以 host:port options; 方式組織在 {} 中。若是後端被代理的只有一臺,也能夠直接寫在 proxy_pass 。
2.2.4 location
http服務中,某些特定的URL對應的一系列配置項。
關於location匹配規則的寫法,能夠說尤其關鍵且基礎的,參考文章 nginx配置location總結及rewrite規則寫法;
2.3 其它
2.3.1 訪問控制 allow/deny
Nginx 的訪問控制模塊默認就會安裝,並且寫法也很是簡單,能夠分別有多個allow,deny,容許或禁止某個ip或ip段訪問,依次知足任何一個規則就中止往下匹配。如:
1 2 3 4 5 6 7 8 9 10 |
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 來爲訪問的路徑設置登陸密碼:
1 2 3 4 5 6 7 |
# htpasswd -c htpasswd admin New passwd: Re-type new password: Adding password for user admin
# htpasswd htpasswd admin //修改admin密碼 # htpasswd htpasswd sean //多添加一個認證用戶 |
這樣就生成了默認使用CRYPT加密的密碼文件。打開上面nginx-status的兩行註釋,重啓nginx生效。
2.3.2 列出目錄 autoindex
Nginx默認是不容許列出整個目錄的。如需此功能,打開nginx.conf文件,在location,server 或 http段中加入autoindex on;,另外兩個參數最好也加上去:
1 2 3 4 5 6 |
location /images { root /var/www/nginx-default/images; autoindex on; autoindex_exact_size off; autoindex_localtime on; } |
參考