Nginx服務器搭建和基本配置實例分析

nginx(engine x) 是一個高性能的 http 服務器和反向代理服務器,這款軟件開發的目的是為了解決 c10k 問題。

nginx 的架構利用了許多現代操作系統的特性,以實現一個高性能的 http 服務器。例如在 linux 系統上,nginx 使用了 epoll,sendfile,file aio,directio 等機制,使得 nginx 不僅性能高效,而且資源占用率非常低,官方宣稱 nginx 維持 10000 個非活動的 http keep-alive 連接僅需要 2.5m 內存。
Nginx服務器搭建和基本配置實例分析
nginx會按需同時運行多個進程:一個主進程(master)和幾個工作進程(worker),配置了緩存時還會有緩存加載器進程(cache loader)和緩存管理器進程(cache manager)等。所有進程均是僅含有一個線程,并主要通過“共享內存”的機制實現進程間通信。主進程以 root 用戶身份運行,而worker、cache loader 和 cache manager 均應以非特權用戶身份運行。

1. 安裝 nginx
在 centos6 版本的 epel 源中,已經加入了 nginx 的 rpm 包,不過此 rpm 包版本較低。如果需要更新版本,可以使用官方制作的 rpm 包,或者使用源碼包編譯安裝。

還可以使用一些二次開發功能增強的 nginx 版本,例如淘寶的 tengine 和 openresty 都是不錯的選擇。

1.1 常用編譯參數

–prefix=path:指定 nginx 的安裝目錄
–conf-path=path:指定 nginx.conf 配置文件路徑
–user=name:nginx 工作進程的用戶
–with-pcre:開啟 pcre 正則表達式的支持
–with-http_ssl_module:啟動 ssl 的支持
–with-http_stub_status_module:用于監控 nginx 的狀態
–with-http-realip_module:允許改變客戶端請求頭中客戶端 ip 地址
–with-file-aio:啟用 file aio
–add-module=path:添加第三方外部模塊
這里提供一個完整的編譯方案:

–prefix=/usr/local/nginx
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–http-client-body-temp-path=/var/tmp/nginx/client_body
–http-proxy-temp-path=/var/tmp/nginx/proxy
–http-fastcgi-temp-path=/var/tmp/nginx/fastcgi
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
–pid-path=/var/run/nginx.pid
–lock-path=/var/lock/nginx
–user=nginx
–group=nginx
–with-file-aio
–with-http_ssl_module
–with-http_realip_module
–with-http_sub_module
–with-http_gzip_static_module
–with-http_stub_status_module
–with-pcre
1.2 nginx 的啟動和關閉

啟動 nginx:

#?nginx?-c?/etc/nginx/nginx.conf

關閉 nginx

#?nginx?-s?stop

重讀配置文件

#?nginx?-s?reload #?pkill?-hup?nginx

重新打開日志文件

#?nginx?-s?reopen #?pkill?-usr1?nginx

還可以下載 nginx rpm 包中的 /etc/init.d/nginx 文件,修改路徑后即可使用:

#?service?nginx?{start|stop|status|restart|reload|configtest|}

2. nginx.conf 配置文件

nginx 配置文件主要分成四部分:main(全局設置)、http(http 的通用設置)、server(虛擬主機設置)、location(匹配 url 路徑)。還有一些其他的配置段,如 event,upstream 等。

2.1 通用設置

user nginx
指定運行 nginx workre 進程的用戶和組

worker_rlimit_nofile #
指定所有 worker 進程能夠打開的最大文件數

worker_cpu_affinity
設置 worker 進程的 cpu 粘性,以避免進程在 cpu 間切換帶來的性能消耗。如 worker_cpu_affinity 0001 0010 0100 1000;(四核)

worker_processes 4
worker 工作進程的個數,這個值可以設置為與 cpu 數量相同,如果開啟了 ssl 和 gzip,那么可以適當增加此數值

worker_connections 1000
單個 worker 進程能接受的最大并發連接數,放在 event 段中

error_log logs/error.log info
錯誤日志的存放路徑和記錄級別

use epoll
使用 epoll 事件模型,放在 event 段中

2.2 http 服務器

server {}:
定義一個虛擬主機

listen 80;
定義監聽的地址和端口,默認監聽在本機所有地址上

server_name name […];
定義虛擬主機名,可以使用多個名稱,還可以使用正則表達式或通配符。

sendfile on
開啟 sendfile 調用來快速的響應客戶端

keepalive_timeout 65
長連接超時時間,單位是秒。

send_timeout
指定響應客戶端的超時時間

client_max_body_size 10m
允許客戶端請求的實體最大大小

root path
設置請求 url 所對應資源所在文件系統上的根目錄

location [ = | ~ | ~* | ^~ ] uri { … }
設置一個 uri 匹配路徑
=:精確匹配
~:正則表達式匹配,區分字符大小寫
~*:正則表達式匹配,不區分字符大小寫
^~:uri 的前半部分匹配,且不實用正則表達式
優先級:
= > location 完整路徑 > ^~ > ~ > ~* > location 起始路徑 > location /

allow 和 deny
基于 ip 訪問控制,如:

僅允許 192.168.0.0/24 網段客戶端訪問

allow 192.168.0.0/24;
deny all;
stub_status on
開啟狀態顯式,僅能用于 location 中:
開啟狀態顯式頁面

location?/status?{ stub_status?on; allow?172.16.0.0/16; deny?all; }

rewrite
url 重寫,可以使用多種標記
例如:

rewrite ^/images/(.*.jpg)$ /imgs/$1 break;
可用的 flag:
– last:重寫完成后,繼續匹配其他 rewrite 規則
– break:重寫完成后不再繼續匹配
– redirect:返回 302 重定向(臨時重定向),客戶端對重定向的 url 發起新的請求
– permanent:返回 301 重定向(永久重定向),客戶端對重定向的 url 發起新的請求

一個 server 配置示例:

server?{ ?listen??80; ?server_name?www.example.com; ?root?/web/htdocs;  ?location?/?{ ??index?index.html?index.htm; ?}  ?location?/status?{ ??stub_status?on; ??allow?10.0.0.0/8; ??deny?all; ??access_log?off; }

2.3 ssl 的配置

啟用一個 ssl 虛擬主機

server?{ ??listen?443; ??server_name?example.com;  ??root?/apps/www; ??index?index.html?index.htm;  ??ssl?on; ??ssl_certificate?/etc/nginx/ssl/nginx.crt; ??ssl_certificate_key?/etc/nginx/ssl/nginx.key;  #??ssl_protocols?sslv3?tlsv1?tlsv1.1?tlsv1.2; #??ssl_ciphers?all:!adh:!export56:rc4+rsa:+high:+medium:+low:+sslv2:+exp; #??ssl_prefer_server_ciphers?on;  }

其中 ssl_certificate 表示 ca 文件,ssl_certificate_key 表示密鑰文件。

如果想把 http 請求強制轉到 https,可以這樣:

server?{ listen??80; server_name?example.me;  return?301?https://$server_name$request_uri; }

2.4 nginx 做負載均衡反向代理

nginx 做反向代理時,后端主機有多臺,可以使用 upstream 定義一個后端主機池,在反向代理時直接使用主機池的名字。在 upstream 中可以定義負載均衡調度算法,權重,健康狀態檢測等參數。

例如:

upstream?backend?{ ?server?172.16.0.1:80?weight=1?max-fails=3?fail_timeout=10; ?server?172.16.0.2:80?weight=1max-fails=3?fail_timeout=10;; }

默認請求下,使用 round-robin 調度算法,并有健康狀態檢查和恢復主機的能力。

ningx 還可以使用這些算法:

ip_hash:基于源地址哈希,主要目的是會話保持
least_conn:基于最少活動連接進行調度
sticky:基于 cookie 進行會話綁定,nginx 會在客戶端第一次訪問時插入路由信息到 cookie 中,或者選擇 cookie 中的某個字段的值作為鍵,以后每次請求將基于此信息進行調度
基于 cookie 的會話綁定共有 cookie,route 和 learn 三種。

例如,基于 cookie name 的調度:

upstream?backend?{ ?server?backend1.example.com; ?server?backend2.example.com;  ?sticky?cookie?srv_id?expires=1h?domain=.example.com?path=/; }

使用此主機組進行反向代理:

location?/?{ ?proxy_pass?http://backend; ?proxy_set_header?host?$host; ?proxy_set_haeder?x-forwared-for?$proxy_add_x_forwarded_for; }

proxy_pass url 指定代理的后端主機,可以指定 “http” 或 “https” 協議,域名可以是 ip 地址,也可以是 upstream 池名字

如果代理指定的是一個 uri 地址,如 http://127.0.0.1/remote,那么將直接被代理至指定 uri,無論請求的 uri 是什么
如果代理指定的一個主機而沒有 uri,如 http://127.0.0.1,客戶端請求的uri將被傳遞至指定域名
如果 location 中使用模式匹配 url,那么 url 也會被傳遞至代理 url 的末端
如果 location 中使用了 uri 重寫,那么 proxy_pass 會使用重寫后的結果進行處理
proxy_set_header header value 對轉發的報文首部進行修改

2.5 反向代理時的緩存相關設定

proxy_cache_path path [levels=levels] keys_zone=name:size

定義磁盤緩存路徑,nignx 的緩存是以鍵值方式存放的,keys_zone 用于指定鍵存放的內存空間名字和大小,對應的值則存放在 path 指定的路徑中。levels 可以指定緩存存放路徑的級數和名稱字符數。此設置只能在 http 段中定義。

如:

proxy_cache_path?/var/cache/nginx/proxy?levels=1:2?keys_zone=one:10m;

proxy_cache_valid [code …] time 指定不同響應碼的內容的緩存時間

如:

proxy_cache_valid?200?302?10m; proxy_cache_valid?404??1m; proxy_cache_valid?any??1m;

proxy_cache_method method 定義哪些方法的請求結果可以被緩存,如:

proxy_cache_method?get; proxy_cache_method?head;

proxy_cache name 指定使用預先定義的緩存空間用于緩存

2.6 fastcgi 代理的設置

使用 fastcgi 時,設置代理的方法同 porxy_pass 類似,同時還可以使用 fastcgi 緩存,設置的方法也和 proxy_cache 類似。

location?~?.php$?{ ?root???/web/htdocs; ?fastcgi_pass?127.0.0.1:9000; ?fastcgi_index?index.php; ?fastcgi_param?script_filename?$document_root$fastcgi_script_name; ?include??fastcgi_params; }

3. 一些常用內置變量

$arg_name:請求 uri 中的 name 參數至
$args:請求 uri 的所有參數,和 $query_string 相同
$uri:當前請求的 uri,不帶參數
$request_uri:請求的 uri,帶完整參數
$host:http 請求報文中 host 首部,如果沒有 host 首部,則以處理此請求的虛擬主機的主機名替代
$hostname:nginx 服務運行在主機的主機名
$remote_addr:客戶端 ip
$remote_port:客戶端 port
$remote_user:使用用戶認證時客戶端用戶輸入的用戶名
$request_filename:用戶請求中的 uri 經過本地 root? 或 alias 轉換后映射的本地的文件路徑
$request_method:請求方法
$server_addr:服務器地址
$server_name:服務器名稱
$server_port:服務器端口
$server_protocol:服務器向客戶端發送響應時的協議,如 http/1.1,http/1.0
$scheme:在請求中使用的 scheme,如 https://www.magedu.com/ 中的 https
$http_name:匹配請求報文中的指定 header,如 $http_host 匹配請求報文中的 host 首部
$sent_http_name:匹配響應報文中指定的 header,例如 $sent_content_type 匹配響應報文中的 content-type 首部
$status:響應狀態

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享