nginx中文手冊內容說明

1.什麼是nginx?javascript

Nginx 是一個高性能的 Web 和反向代理服務器, 它具備有不少很是優越的特性:php

做爲 Web 服務器:相比 Apache,Nginx 使用更少的資源,支持更多的併發鏈接,體現更高的效率,這點使 Nginx 尤爲受到虛擬主機提供商的歡迎。可以支持高達 50,000 個併發鏈接數的響應,感謝 Nginx 爲咱們選擇了 epoll and kqueue 做爲開發模型.css

做爲負載均衡服務器:Nginx 既能夠在內部直接支持 Rails 和 PHP,也能夠支持做爲 HTTP代理服務器 對外進行服務。Nginx 用 C 編寫, 不管是系統資源開銷仍是 CPU 使用效率都比 Perlbal 要好的多。html

做爲郵件代理服務器: Nginx 同時也是一個很是優秀的郵件代理服務器(最先開發這個產品的目的之一也是做爲郵件代理服務器),Last.fm 描述了成功而且美妙的使用經驗。前端

Nginx 安裝很是的簡單,配置文件 很是簡潔(還可以支持perl語法),Bugs很是少的服務器: Nginx 啓動特別容易,而且幾乎能夠作到7*24不間斷運行,即便運行數個月也不須要從新啓動。你還可以在 不間斷服務的狀況下進行軟件版本的升級。java

二、如何源碼編譯安裝nginx?
node

 

nginx可使用各平臺的默認包來安裝,本文是介紹使用源碼編譯安裝,包括具體的編譯參數信息。mysql

正式開始前,編譯環境gcc g++ 開發庫之類的須要提早裝好,這裏默認你已經裝好。linux

ububtu平臺編譯環境可使用如下指令nginx

apt-get install build-essential apt-get install libtool

centos平臺編譯環境使用以下指令

安裝make:

yum -y install gcc automake autoconf libtool make

安裝g++:

yum install gcc gcc-c++

下面正式開始
---------------------------------------------------------------------------
通常咱們都須要先裝pcre, zlib,前者爲了重寫rewrite,後者爲了gzip壓縮。
1.選定源碼目錄
能夠是任何目錄,本文選定的是/usr/local/src

cd /usr/local/src

2.安裝PCRE庫
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下載最新的 PCRE 源碼包,使用下面命令下載編譯和安裝 PCRE 包:

cd /usr/local/src wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz  tar -zxvf pcre-8.34.tar.gz cd pcre-8.34 ./configure make make install

3.安裝zlib庫
http://zlib.net/zlib-1.2.8.tar.gz 下載最新的 zlib 源碼包,使用下面命令下載編譯和安裝 zlib包:

cd /usr/local/src wget http://zlib.net/zlib-1.2.8.tar.gz tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8 ./configure make make install

4.安裝ssl(某些vps默認沒裝ssl)

cd /usr/local/src wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz tar -zxvf openssl-1.0.1c.tar.gz

5.安裝nginx

Nginx 通常有兩個版本,分別是穩定版和開發版,您能夠根據您的目的來選擇這兩個版本的其中一個,下面是把 Nginx 安裝到 /usr/local/nginx 目錄下的詳細步驟:

cd /usr/local/src wget http://nginx.org/download/nginx-1.4.2.tar.gz tar -zxvf nginx-1.4.2.tar.gz cd nginx-1.4.2 ./configure --sbin-path=/usr/local/nginx/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-http_ssl_module \ --with-pcre=/usr/local/src/pcre-8.34 \ --with-zlib=/usr/local/src/zlib-1.2.8 \ --with-openssl=/usr/local/src/openssl-1.0.1c make make install

--with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源碼路徑。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源碼路徑。

安裝成功後 /usr/local/nginx 目錄下以下

fastcgi.conf koi-win nginx.conf.default fastcgi.conf.default logs scgi_params fastcgi_params mime.types scgi_params.default fastcgi_params.default mime.types.default uwsgi_params html nginx uwsgi_params.default koi-utf nginx.conf win-utf

6.啓動
確保系統的 80 端口沒被其餘程序佔用,運行/usr/local/nginx/nginx 命令來啓動 Nginx,

netstat -ano|grep 80

若是查不到結果後執行,有結果則忽略此步驟(ubuntu下必須用sudo啓動,否則只能在前臺運行)

sudo /usr/local/nginx/nginx

打開瀏覽器訪問此機器的 IP,若是瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功。

-----------------------------------------------------
到這裏nginx就安裝完成了,若是隻是處理靜態html就不用繼續安裝了

若是你須要處理php腳本的話,還須要安裝php-fpm

下面安裝排錯

附:可能遇到的錯誤和一些幫助信息

1.1編譯pcre錯誤

libtool: compile: unrecognized option `-DHAVE_CONFIG_H' libtool: compile: Try `libtool --help' for more information. make[1]: *** [pcrecpp.lo] Error 1 make[1]: Leaving directory `/usr/local/src/pcre-8.34' make: *** [all] Error 2

解決辦法:安裝g++,別忘了從新configure

apt-get install g++ apt-get install build-essential make clean ./configure make

1.2 make出錯

make: *** No rule to make target `build', needed by `default'.  Stop. ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option.

按照第4步的安裝方法或
ubuntu下

apt-get install openssl apt-get install libssl-dev

centos下

yum -y install openssl openssl-devel

2.nginx編譯選項

make是用來編譯的,它從Makefile中讀取指令,而後編譯。

make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。

configure命令是用來檢測你的安裝平臺的目標特徵的。它定義了系統的各個方面,包括nginx的被容許使用的鏈接處理的方法,好比它會檢測你是否是有CC或GCC,並非須要CC或GCC,它是個shell腳本,執行結束時,它會建立一個Makefile文件。nginx的configure命令支持如下參數:

  • --prefix=path    定義一個目錄,存放服務器上的文件 ,也就是nginx的安裝目錄。默認使用 /usr/local/nginx。
  • --sbin-path=path 設置nginx的可執行文件的路徑,默認爲  prefix/sbin/nginx.
  • --conf-path=path  設置在nginx.conf配置文件的路徑。nginx容許使用不一樣的配置文件啓動,經過命令行中的-c選項。默認爲prefix/conf/nginx.conf.
  • --pid-path=path  設置nginx.pid文件,將存儲的主進程的進程號。安裝完成後,能夠隨時改變的文件名 , 在nginx.conf配置文件中使用 PID指令。默認狀況下,文件名 爲prefix/logs/nginx.pid.
  • --error-log-path=path 設置主錯誤,警告,和診斷文件的名稱。安裝完成後,能夠隨時改變的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默認狀況下,文件名 爲prefix/logs/error.log.
  • --http-log-path=path  設置主請求的HTTP服務器的日誌文件的名稱。安裝完成後,能夠隨時改變的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默認狀況下,文件名 爲prefix/logs/access.log.
  • --user=name  設置nginx工做進程的用戶。安裝完成後,能夠隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的用戶名是nobody。
  • --group=name  設置nginx工做進程的用戶組。安裝完成後,能夠隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的爲非特權用戶。
  • --with-select_module --without-select_module 啓用或禁用構建一個模塊來容許服務器使用select()方法。該模塊將自動創建,若是平臺不支持的kqueue,epoll,rtsig或/dev/poll。
  • --with-poll_module --without-poll_module 啓用或禁用構建一個模塊來容許服務器使用poll()方法。該模塊將自動創建,若是平臺不支持的kqueue,epoll,rtsig或/dev/poll。
  • --without-http_gzip_module — 不編譯壓縮的HTTP服務器的響應模塊。編譯並運行此模塊須要zlib庫。
  • --without-http_rewrite_module  不編譯重寫模塊。編譯並運行此模塊須要PCRE庫支持。
  • --without-http_proxy_module — 不編譯http_proxy模塊。
  • --with-http_ssl_module — 使用https協議模塊。默認狀況下,該模塊沒有被構建。創建並運行此模塊的OpenSSL庫是必需的。
  • --with-pcre=path — 設置PCRE庫的源碼路徑。PCRE庫的源碼(版本4.4 - 8.30)須要從PCRE網站下載並解壓。其他的工做是Nginx的./ configure和make來完成。正則表達式使用在location指令和 ngx_http_rewrite_module 模塊中。
  • --with-pcre-jit —編譯PCRE包含「just-in-time compilation」(1.1.12中, pcre_jit指令)。
  • --with-zlib=path —設置的zlib庫的源碼路徑。要下載從 zlib(版本1.1.3 - 1.2.5)的並解壓。其他的工做是Nginx的./ configure和make完成。ngx_http_gzip_module模塊須要使用zlib 。
  • --with-cc-opt=parameters — 設置額外的參數將被添加到CFLAGS變量。例如,當你在FreeBSD上使用PCRE庫時須要使用:--with-cc-opt="-I /usr/local/include。.如須要須要增長 select()支持的文件數量:--with-cc-opt="-D FD_SETSIZE=2048".
  • --with-ld-opt=parameters —設置附加的參數,將用於在連接期間。例如,當在FreeBSD下使用該系統的PCRE庫,應指定:--with-ld-opt="-L /usr/local/lib".

典型實例(下面爲了展現須要寫在多行,執行時內容須要在同一行)

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-4.4 --with-zlib=../zlib-1.1.3

3.nginx簡單配置詳解(從welcome to nginx開始上手)
#運行用戶 user nobody; #啓動進程,一般設置成和cpu的數量相等 worker_processes 1; #全局錯誤日誌及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #工做模式及鏈接數上限 events { #epoll是多路複用IO(I/O Multiplexing)中的一種方式, #僅用於linux2.6以上內核,能夠大大提升nginx的性能 use epoll; #單個後臺worker process進程的最大併發連接數  worker_connections 1024; # 併發總數是 worker_processes 和 worker_connections 的乘積 # 即 max_clients = worker_processes * worker_connections # 在設置了反向代理的狀況下,max_clients = worker_processes * worker_connections / 4 爲何 # 爲何上面反向代理要除以4,應該說是一個經驗值 # 根據以上條件,正常狀況下的Nginx Server能夠應付的最大鏈接數爲:4 * 8000 = 32000 # worker_connections 值的設置跟物理內存大小有關 # 由於併發受IO約束,max_clients的值須小於系統能夠打開的最大文件數 # 而系統能夠打開的最大文件數和內存大小成正比,通常1GB內存的機器上能夠打開的文件數大約是10萬左右 # 咱們來看看360M內存的VPS能夠打開的文件句柄數是多少: # $ cat /proc/sys/fs/file-max # 輸出 34336 # 32000 < 34336,即併發鏈接總數小於系統能夠打開的文件句柄總數,這樣就在操做系統能夠承受的範圍以內 # 因此,worker_connections 的值需根據 worker_processes 進程數目和系統能夠打開的最大文件總數進行適當地進行設置 # 使得併發總數小於操做系統能夠打開的最大文件數目 # 其實質也就是根據主機的物理CPU和內存進行配置 # 固然,理論上的併發總數可能會和實際有所誤差,由於主機還有其餘的工做進程須要消耗系統資源。 # ulimit -SHn 65535 } http { #設定mime類型,類型由mime.type文件定義 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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件, #對於普通應用,必須設爲 on, #若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off, #以平衡磁盤與網絡I/O處理速度,下降系統的uptime. sendfile on; #tcp_nopush on; #鏈接超時時間 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #開啓gzip壓縮 gzip on; gzip_disable "MSIE [1-6]."; #設定請求緩衝 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #設定虛擬主機配置 server { #偵聽80端口 listen 80; #定義使用 www.nginx.cn訪問 server_name www.nginx.cn; #定義服務器的默認網站根目錄位置 root html; #設定本虛擬主機的訪問日誌 access_log logs/nginx.access.log main; #默認請求 location / { #定義首頁索引文件的名稱 index index.php index.html index.htm; } # 定義錯誤提示頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { } #靜態文件,nginx本身處理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { #過時30天,靜態文件不怎麼更新,過時能夠設大一點, #若是頻繁更新,則能夠設置得小一點。 expires 30d; } #PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置. location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #禁止訪問 .htxxx 文件 location ~ /.ht { deny all; } } }

4.nginx平滑升級

  1. Nginx命令行選項
  2. 啓動、中止、重啓nginx命令
  3. 如何從新加載nginx配置文件
  4. nginx程序不重啓平滑升級可執行文件

 

nginx命令行參數

不像許多其餘軟件系統,Nginx 僅有幾個命令行參數,徹底經過配置文件來配置

-c </path/to/config> 爲 Nginx 指定一個配置文件,來代替缺省的。

-t 不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。

-v 顯示 nginx 的版本。

-V 顯示 nginx 的版本,編譯器版本和配置參數。

 

nginx控制信號

可使用信號系統來控制主進程。默認,nginx 將其主進程的 pid 寫入到 /usr/local/nginx/nginx.pid 文件中。經過傳遞參數給 ./configure 或使用 pid 指令,來改變該文件的位置。

主進程能夠處理如下的信號:

TERM, INT 快速關閉
QUIT 從容關閉
HUP 重載配置
用新的配置開始新的工做進程
從容關閉舊的工做進程
USR1 從新打開日誌文件
USR2 平滑升級可執行程序。
WINCH 從容關閉工做進程

儘管你沒必要本身操做工做進程,可是,它們也支持一些信號:

TERM, INT 快速關閉
QUIT 從容關閉
USR1 從新打開日誌文件

 

nginx 啓動、中止、重啓命令

nginx啓動

sudo /usr/local/nginx/nginx     (nginx二進制文件絕對路徑,能夠根據本身安裝路徑實際決定)

 

nginx從容中止命令,等全部請求結束後關閉服務

ps -ef |grep nginx

kill -QUIT  nginx主進程號

 

nginx 快速中止命令,馬上關閉nginx進程

ps -ef |grep nginx

kill -TERM nginx主進程號 

若是以上命令無論用,能夠強制中止

kill -9 nginx主進程號

 

若是嫌麻煩能夠不用查看進程號,直接使用命令進行操做
其中/usr/local/nginx/nginx.pid 爲nginx.conf中pid命令設置的參數,用來存放nginx主進程號的文件
kill -信號類型(HUP|TERM|QUIT) `cat /usr/local/nginx/nginx.pid` 
例如

kill -QUIT `cat /usr/local/nginx/nginx.pid`

nginx重啓命令

nginx重啓能夠分紅幾種類型

1.簡單型,先關閉進程,修改你的配置後,重啓進程。
kill -QUIT `cat /usr/local/nginx/nginx.pid`
sudo /usr/local/nginx/nginx
2.從新加載配置文件,不重啓進程,不會中止處理請求
3.平滑更新nginx二進制,不會中止處理請求

 

使用信號加載新的配置

Nginx 支持幾個信號,能在它運行時控制其操做。其中最普通的是 15 ,用來停止運行的進程:

# <strong>ps aux | egrep '(PID|nginx)'</strong> USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND root      2213  0.0  0.0   6784  2036 ?        Ss   03:01   0:00 nginx: master process /usr/sbin/nginx -/etc/nginx/nginx.conf # <strong>kill -15 2213</strong>

而最有趣的是能平滑改變 nginx 配置的選項(請注意,在重載前,要先測試一下配置文件):

 

#<strong> nginx -t -c /etc/nginx/nginx.conf</strong> 2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf syntax is ok 2006/09/16 13:07:10 [info] 15686#0: the configuration file /etc/nginx/nginx.conf was tested successfully #<strong> ps aux | egrep '(PID|nginx)'</strong> USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND root      2213  0.0  0.0   6784  2036 ?        Ss   03:01   0:00 nginx: master process /usr/sbin/nginx -/etc/nginx/nginx.conf <strong># kill -HUP 2213</strong>

當 nginx 接收到 HUP 信號,它會嘗試先解析配置文件(若是指定配置文件,就使用指定的,不然使用默認的),成功的話,就應用新的配置文件(例如:從新打開日誌文件或監聽的套接 字)。以後,nginx 運行新的工做進程並從容關閉舊的工做進程。通知工做進程關閉監聽套接字可是繼續爲當前鏈接的客戶提供服務。全部客戶端的服務完成後,舊的工做進程被關閉。 若是新的配置文件應用失敗,nginx 將繼續使用舊的配置進行工做。

 

平滑升級到新的二進制代碼

你能夠在不中斷服務的狀況下 - 新的請求也不會丟失,使用新的 nginx 可執行程序替換舊的(當升級新版本或添加/刪除服務器模塊時)。

首先,使用新的可執行程序替換舊的(最好作好備份),而後,發送 USR2 (kill -USR2 pid)信號給主進程。主進程將重命名它的 .pid 文件爲 .oldbin (好比:/usr/local/nginx/logs/nginx.pid.oldbin),而後執行新的可執行程序,依次啓動新的主進程和新的工做進程:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND 33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx 33134 33126 nobody   0.0  1368 kqread nginx: worker process (nginx) 33135 33126 nobody   0.0  1380 kqread nginx: worker process (nginx) 33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx) 36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

在這時,兩個 nginx 實例會同時運行,一塊兒處理輸入的請求。要逐步中止舊的實例,你必須發送 WINCH 信號給舊的主進程,而後,它的工做進程就將開始從容關閉:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND 33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx 33135 33126 nobody   0.0  1380 kqread nginx: worker process is shutting down (nginx) 36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

一段時間後,舊的工做進程處理了全部已鏈接的請求後退出,就僅由新的工做進程來處理輸入的請求了:

 

  PID  PPID USER    %CPU   VSZ WCHAN  COMMAND 33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx 36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx 36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx) 36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

這時,由於舊的服務器還還沒有關閉它監聽的套接字,因此,經過下面的幾步,你仍能夠恢復舊的服務器:

  • 發送 HUP 信號給舊的主進程 - 它將在不重載配置文件的狀況下啓動它的工做進程
  • 發送 QUIT 信號給新的主進程,要求其從容關閉其工做進程
  • 發送 TERM 信號給新的主進程,迫使其退出
  • 若是由於某些緣由新的工做進程不能退出,向其發送 KILL 信號

新的主進程退出後,舊的主進程會由移除 .oldbin 前綴,恢復爲它的 .pid 文件,這樣,一切就都恢復到升級以前了。

若是嘗試升級成功,而你也但願保留新的服務器時,發送 QUIT 信號給舊的主進程使其退出而只留下新的服務器運行:

 

      PID  PPID USER    %CPU   VSZ WCHAN  COMMAND     36264     1 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx     36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)     36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)     36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

5.nginx重要指令之location

location匹配命令

~      #波浪線表示執行一個正則匹配,區分大小寫
~*    #表示執行一個正則匹配,不區分大小寫
^~    #^~表示普通字符匹配,若是該選項匹配,只匹配該選項,不匹配別的選項,通常用來匹配目錄
=      #進行普通字符精確匹配
@     #"@" 定義一個命名的 location,使用在內部定向時,例如 error_page, try_files

 

location 匹配的優先級(與location在配置文件中的順序無關)
= 精確匹配會第一個被處理。若是發現精確匹配,nginx中止搜索其餘匹配。
普通字符匹配,正則表達式規則和長的塊規則將被優先和查詢匹配,也就是說若是該項匹配還需去看有沒有正則表達式匹配和更長的匹配。
^~ 則只匹配該規則,nginx中止搜索其餘匹配,不然nginx會繼續處理其餘location指令。
最後匹配理帶有"~"和"~*"的指令,若是找到相應的匹配,則nginx中止搜索其餘匹配;當沒有正則表達式或者沒有正則表達式被匹配的狀況下,那麼匹配程度最高的逐字匹配指令會被使用。

location 優先級官方文檔

  1. Directives with the = prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
  3. Regular expressions, in order of definition in the configuration file.
  4. If #3 yielded a match, that result is used. Else the match from #2 is used.
  1. =前綴的指令嚴格匹配這個查詢。若是找到,中止搜索。
  2. 全部剩下的常規字符串,最長的匹配。若是這個匹配使用^〜前綴,搜索中止。
  3. 正則表達式,在配置文件中定義的順序。
  4. 若是第3條規則產生匹配的話,結果被使用。不然,如同從第2條規則被使用。

 

例如

location  = / { # 只匹配"/". [ configuration A ] } location / { # 匹配任何請求,由於全部請求都是以"/"開始 # 可是更長字符匹配或者正則表達式匹配會優先匹配 [ configuration B ] } location ^~ /images/ { # 匹配任何以 /images/ 開始的請求,並中止匹配 其它location [ configuration C ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配以 gif, jpg, or jpeg結尾的請求. # 可是全部 /images/ 目錄的請求將由 [Configuration C]處理. [ configuration D ] }

請求URI例子:

  • / -> 符合configuration A
  • /documents/document.html -> 符合configuration B
  • /images/1.gif -> 符合configuration C
  • /documents/1.jpg ->符合 configuration D

@location 例子
error_page 404 = @fetch;

location @fetch(
proxy_pass http://fetch;
)

6.nginx重要指令之upstream

7.nginx rewrite功能

nginx經過ngx_http_rewrite_module模塊支持url重寫、支持if條件判斷,但不支持else。

該模塊須要PCRE支持,應在編譯nginx時指定PCRE源碼目錄,nginx安裝方法

nginx rewrite指令執行順序:

1.執行server塊的rewrite指令(這裏的塊指的是server關鍵字後{}包圍的區域,其它xx塊相似)
2.執行location匹配
3.執行選定的location中的rewrite指令
若是其中某步URI被重寫,則從新循環執行1-3,直到找到真實存在的文件

若是循環超過10次,則返回500 Internal Server Error錯誤

 

break指令

語法:break;
默認值:無
做用域:server,location,if

中止執行當前虛擬主機的後續rewrite指令集
break指令實例:

 if ($slow) {      limit_rate 10k;      break;  }

if指令

語法:if(condition){...}
默認值:無
做用域:server,location
對給定的條件condition進行判斷。若是爲真,大括號內的rewrite指令將被執行。
if條件(conditon)能夠是以下任何內容:

  • 一個變量名;false若是這個變量是空字符串或者以0開始的字符串;
  • 使用= ,!= 比較的一個變量和字符串
  • 是用~, ~*與正則表達式匹配的變量,若是這個正則表達式中包含},;則整個表達式須要用" 或' 包圍
  • 使用-f ,!-f 檢查一個文件是否存在
  • 使用-d, !-d 檢查一個目錄是否存在
  • 使用-e ,!-e 檢查一個文件、目錄、符號連接是否存在
  • 使用-x , !-x 檢查一個文件是否可執行

if指令實例

 if ($http_user_agent ~ MSIE) {      rewrite ^(.*)/msie/$1 break;  }  if ($http_cookie ~* "id=([^;]+)(?:;|$)") {      set $id $1;  }  if ($request_method = POST) {      return 405;  }  if ($slow) {      limit_rate 10k;  }  if ($invalid_referer) {      return 403;  }

return指令

語法:return code;

return code URL;

return URL;
默認值:無
做用域:server,location,if

中止處理並返回指定狀態碼(code)給客戶端。
非標準狀態碼444表示關閉鏈接且不給客戶端發響應頭。
從0.8.42版本起,return 支持響應URL重定向(對於301,302,303,307),或者文本響應(對於其餘狀態碼).
對於文本或者URL重定向能夠包含變量

rewrite指令

語法:rewrite regex replacement [flag];
默認值:無
做用域:server,location,if
若是一個URI匹配指定的正則表達式regex,URI就按照replacement重寫。
rewrite按配置文件中出現的順序執行。flags標誌能夠中止繼續處理。
若是replacement以"http://"或"https://"開始,將再也不繼續處理,這個重定向將返回給客戶端。
flag能夠是以下參數
last 中止處理後續rewrite指令集,而後對當前重寫的新URI在rewrite指令集上從新查找。
break 中止處理後續rewrite指令集,並不在從新查找,可是當前location內剩餘非rewrite語句和location外的的非rewrite語句能夠執行。
redirect 若是replacement不是以http:// 或https://開始,返回302臨時重定向
permant 返回301永久重定向
最終完整的重定向URL包括請求scheme(http://,https://等),請求的server_name_in_redirect和 port_in_redirec三部分 ,說白了也就是http協議 域名 端口三部分組成。

rewrite實例

 server {      ...      rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;      rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;      return 403;      ...  }

若是這些rewrite放到 「/download/」 location以下所示, 那麼應使用break而不是last , 使用last將循環10次匹配,而後返回 500錯誤:

 location /download/ {      rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;      rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;      return 403;  }

對於重寫後的URL(replacement)包含原請求的請求參數,原URL的?後的內容。若是不想帶原請求的參數 ,能夠在replacement後加一個問號。以下,咱們加了一個自定義的參數user=$1,而後在結尾處放了一個問號?,把原請的參數去掉。

rewrite ^/users/(.*)/show?user=$1? last;

若是正則表達regex式中包含 「}」 或 「;」, 那麼整個表達式須要用雙引號或單引號包圍.

rewrite_log指令


語法:rewrite_log on|off;
默認值:rewrite_log off;
做用域:http,server,location,if
開啓或關閉以notice級別打印rewrite處理日誌到error log文件。

nginx打開rewrite log例子

rewrite_log on;
error_log logs/xxx.error.log notice;

1.打開rewrite on
2.把error log的級別調整到 notice

set指令


語法:set variable value;
默認值:none
做用域:server,location,if
定義一個變量並賦值,值能夠是文本,變量或者文本變量混合體。

uninitialized_variable_warn指令


語法:uninitialized_variable_warn on | off;
默認值:uninitialized_variable_warn on
做用域:http,server,location,if

控制是否輸出爲初始化的變量到日誌

8.nginx的虛擬主機功能(nginx多站點,綁定多個域名)

兩個虛擬主機(純靜態-html 支持) - Two Virtual Hosts, Serving Static Files

 

http {     server {         listen          80;         server_name     www.domain1.com;         access_log      logs/domain1.access.log main;         location / {             index index.html;             root  /var/www/domain1.com/htdocs;         }     }     server {         listen          80;         server_name     www.domain2.com;         access_log      logs/domain2.access.log main;         location / {             index index.html;             root  /var/www/domain2.com/htdocs;         }     } }

 

虛擬主機標準配置(簡化) - A Default Catchall Virtual Host

 

http {     server {         listen          80 default;         server_name     _ *;         access_log      logs/default.access.log main;         location / {             index index.html;             root  /var/www/default/htdocs;         }     } }

 

在父文件夾中創建子文件夾以指向子域名 - Wildcard Subdomains in a Parent Folder

這是一個添加子域名(或是當DNS已指向服務器時添加一個新域名)的簡單方法。須要注意的是,我已經將FCGI配置進該文件了。若是你只想使服務器爲靜態文件服務,能夠直接將FCGI配置信息註釋掉,而後將默認主頁文件變成index.html。

這個簡單的方法比起爲每個域名創建一個 vhost.conf 配置文件來說,只須要在現有的配置文件中增長以下內容:

This is just a really easy way to keep adding new subdomains, or to add new domains automatically when DNS records are pointed at the server. Note that I have included FCGI here as well. If you want to just serve static files, strip out the FCGI config and change the default document to index.html. Rather than creating a new vhost.conf file for every domain, just create one of these:

 

server {         # Replace this port with the right one for your requirements         # 根據你的需求改變此端口         listen       80;  #could also be 1.2.3.4:80 也能夠是1.2.3.4:80的形式         # Multiple hostnames seperated by spaces.  Replace these as well.         # 多個主機名能夠用空格隔開,固然這個信息也是須要按照你的需求而改變的。         server_name  star.yourdomain.com *.yourdomain.com www.*.yourdomain.com;         #Alternately: _ *         #或者可使用:_ * (具體內容參見本維基其餘頁面)         root /PATH/TO/WEBROOT/$host;         error_page  404              http://yourdomain.com/errors/404.html;         access_log  logs/star.yourdomain.com.access.log;         location / {             root   /PATH/TO/WEBROOT/$host/;             index  index.php;         }         # serve static files directly         # 直接支持靜態文件 (愛月說:???從配置上看來不是直接支持啊~有問題有問題~)         location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html){             access_log        off;             expires           30d;         }         location ~ .php$ {           # By all means use a different server for the fcgi processes if you need to           # 若是須要,你能夠爲不一樣的FCGI進程設置不一樣的服務信息           fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;           fastcgi_index  index.php;           fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;           fastcgi_param  QUERY_STRING     $query_string;           fastcgi_param  REQUEST_METHOD   $request_method;           fastcgi_param  CONTENT_TYPE     $content_type;           fastcgi_param  CONTENT_LENGTH   $content_length;           fastcgi_intercept_errors on;         }         location ~ /\.ht {             deny  all;         }      }

9.nginx反向代理功能

nginx做爲web服務器一個重要的功能就是反向代理。

固然你也可使用nginx配置正向代理,本是介紹如何配置nginx的反向代理。

nginx反向代理的指令不須要新增額外的模塊,默認自帶proxy_pass指令,只須要修改配置文件就能夠實現反向代理。

配置前的準備工做,後端跑apache服務的ip和端口,也就是說能夠經過http://ip:port能訪問到你的網站。

而後就能夠新建一個vhost.conf,加入以下內容,記得修改ip和域名爲你的ip和域名。

修改nginx.conf,添加 include quancha.conf 到http{}段, reload nginx就能夠了。

nginx反向代理模板

 


## Basic reverse proxy server ## ## Apache backend for www.quancha.cn ## upstream apachephp { server ip:8080; #Apache } ## Start www.quancha.cn ## server { listen 80; server_name www.quancha.cn; access_log logs/quancha.access.log main; error_log logs/quancha.error.log; root html; index index.html index.htm index.php; ## send request back to apache ## location / { proxy_pass http://apachephp; #Proxy Settings proxy_redirect off; 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; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k; } } ## End www.quancha.cn ##



10.nginx負載均衡功能

使用負載均衡的話,能夠修改配置http節點以下:#設定http服務器,利用它的反向代理功能提供負載均衡支持

http { #設定mime類型,類型由mime.type文件定義 include /etc/nginx/mime.types; default_type application/octet-stream; #設定日誌格式 access_log /var/log/nginx/access.log; #省略上文有的一些配置節點 #。。。。。。。。。。 #設定負載均衡的服務器列表 upstream mysvr { #weigth參數表示權值,權值越高被分配到的概率越大 server 192.168.8.1x:3128 weight=5; #本機上的Squid開啓3128端口,不是必需要squid server 192.168.8.2x:80 weight=1; server 192.168.8.3x:80 weight=6; } upstream mysvr2 { #weigth參數表示權值,權值越高被分配到的概率越大 server 192.168.8.x:80 weight=1; server 192.168.8.x:80 weight=6; } #第一個虛擬服務器 server { #偵聽192.168.8.x的80端口 listen 80; server_name 192.168.8.x; #對aspx後綴的進行負載均衡請求 location ~ .*\.aspx$ { #定義服務器的默認網站根目錄位置 root /root; #定義首頁索引文件的名稱 index index.php index.html index.htm; #請求轉向mysvr 定義的服務器列表 proxy_pass http://mysvr ; #如下是一些反向代理的配置可刪除. 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; #容許客戶端請求的最大單文件字節數 client_max_body_size 10m; #緩衝區代理緩衝用戶端請求的最大字節數, client_body_buffer_size 128k; #nginx跟後端服務器鏈接超時時間(代理鏈接超時) proxy_connect_timeout 90; #鏈接成功後,後端服務器響應時間(代理接收超時) proxy_read_timeout 90; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小 proxy_buffer_size 4k; #proxy_buffers緩衝區,網頁平均在32k如下的話,這樣設置 proxy_buffers 4 32k;

#高負荷下緩衝大小(proxy_buffers*2) proxy_busy_buffers_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳 proxy_temp_file_write_size 64k; } } }
11.nginx web緩存功能

爲何要作web cache,我想你們最主要的是解決流量的壓力。隨着網站流量的提高,若是隻是單臺機器既處理靜態文件,又處理動態腳本,顯然效率很難上升,不能處理日益上漲的流量壓力。與此同時某些網站的頁面內容並非常常變化,所以咱們能夠分兩層架構來組織網站。前端web緩存+後端web服務器,能夠參看這裏配置nginx反向代理配置

前端web緩存有多重方式實現,原理就是隊請求結果頁面靜態化並設置一個超時期限,緩存頁面過時後,新請求到達時從新到後端web服務器獲取內容更新;沒有nginx前比較流行的方法是squid,但squid不能充分利用處理器的多核特性,愈來愈多的網站選用nginx來作前端的web緩存。

要想使用nginx的緩存功能要保證nginx添加了proxy模塊。咱們可使用-V選項(大寫的V,小寫的v是看版本號的)來查看nginx的編譯參數。我使用的是默認的參數編譯的,以下所示:

 

root@SNDA-172-17-12-117:/usr/local/nginx# ./nginx -V
nginx version: nginx/1.2.3
built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.21 --with-zlib=/usr/local/src/zlib-1.2.7

nginx的全部模塊必須在編譯的時候添加,不能再運行的時候動態加載,默認的編譯選項下包含的模塊,若是你不是顯示的用參數關閉它。

nginx默認安裝的模塊以下

模塊名稱 描述 版本 如何禁用
Core Control ports, locations, error pages, aliases, and other essentials.   --without-http
Access Allow/deny based on IP address.   --without-http_access_module
Auth Basic Basic HTTP authentication.   --without-http_auth_basic_module
Auto Index Generates automatic directory listings.   --without-http_autoindex_module
Browser Interpret "User-Agent" string. 0.4.3 --without-http_browser_module
Charset Recode web pages.   --without-http_charset_module
Empty GIF Serve a 1x1 image from memory. 0.3.10 --without-http_empty_gif_module
FastCGI FastCGI Support.   --without-http_fastcgi_module
Geo Set config variables using key/value pairs of IP addresses. 0.1.17 --without-http_geo_module
Gzip Gzip responses.   --without-http_gzip_module
Headers Set arbitrary HTTP response headers.  
Index Controls which files are to be used as index.  
Limit Requests Limit frequency of connections from a client. 0.7.20 --without-http_limit_req_module
Limit Zone Limit simultaneous connections from a client. Deprecated in 1.1.8, use Limit Conn Instead. 0.5.6 --without-http_limit_zone_module
Limit Conn Limit concurrent connections based on a variable.   --without-http_limit_conn_module
Log Customize access logs.  
Map Set config variables using arbitrary key/value pairs. 0.3.16 --without-http_map_module
Memcached Memcached support.   --without-http_memcached_module
Proxy Proxy to upstream servers.   --without-http_proxy_module
Referer Filter requests based on Referer header.   --without-http_referer_module
Rewrite Request rewriting using regular expressions.   --without-http_rewrite_module
SCGI SCGI protocol support. 0.8.42 --without-http_scgi_module
Split Clients Splits clients based on some conditions 0.8.37 --without-http_split_clients_module
SSI Server-side includes.   --without-http_ssi_module
Upstream For load-balancing.   --without-http_upstream_ip_hash_module (ip_hash directive only)
User ID Issue identifying cookies.   --without-http_userid_module
uWSGI uWSGI protocol support. 0.8.40 --without-http_uwsgi_module
X-Accel X-Sendfile-like module.  

proxy模塊中經常使用的指令時proxy_pass和proxy_cache.

nginx的web緩存功能的主要是由proxy_cache、fastcgi_cache指令集和相關指令集完成,proxy_cache指令負責反向代理緩存後端服務器的靜態內容,fastcgi_cache主要用來處理FastCGI動態進程緩存(這裏我不是很清楚這兩個指令的區別,好像功能上都差很少,尤爲後面這句話的意思,是我翻譯過來的)。

確認proxy模塊安裝好後,下面對nginx的配置文件進行設置,重點部分如標紅字體所示。

這是個人nginx.conf配置文件。

user www-data;
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" "$host"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#Compression Settings
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Some version of IE 6 don't handle compression well on some mime-types,
# so just disable for them
gzip_disable "MSIE [1-6].(?!.*SV1)";
# Set a vary header so downstream proxies don't send cached gzipped
# content to IE6
gzip_vary on;
#end gzip

#cache begin
proxy_buffering on;
proxy_cache_valid any 10m;
proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /data/temp;
proxy_buffer_size 4k;
proxy_buffers 100 8k;
#cache end

## Basic reverse proxy server ##
## Apache (vm02) backend for www.example.com ##
upstream apachephp {
server www.quancha.cn:8080; #Apache1
}

## Start www.quancha.cn ##
server {
listen 80;
server_name *.quancha.cn;

access_log logs/quancha.access.log main;
error_log logs/quancha.error.log;
root html;
index index.html index.htm index.php;

## send request back to apache1 ##
location / {
proxy_pass http://apachephp;
proxy_cache my-cache;
proxy_cache_valid 200;

#Proxy Settings
proxy_redirect off;
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;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
##End Proxy Settings
}
}
## End www.quancha.cn ##

}

配置文件中以proxy_開頭的指令咱們大均可以字面意思獲得理解。請務必注意一點proxy_cache_path和proxy_temp_path設置的目錄須要在同一分區,由於它們之間是硬連接的關係。

最後啓動nginx,來迎接着激動人心的時刻吧。我已經火燒眉毛了。若是文章哪裏有問題或者你遇到了什麼麻煩,能夠留言讓我知道。

12.nginx php-fpm 配置

nginx自己不能處理PHP,它只是個web服務器,當接收到請求後,若是是php請求,則發給php解釋器處理,並把結果返回給客戶端。

nginx通常是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並返回被nginx

本文以php-fpm爲例介紹如何使nginx支持PHP

1、編譯安裝php-fpm

什麼是PHP-FPM

PHP-FPM是一個PHP FastCGI管理器,是隻用於PHP的,能夠在 http://php-fpm.org/download下載獲得.

PHP-FPM實際上是PHP源代碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP後纔可使用。

新版PHP已經集成php-fpm了,再也不是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進程管理方式,能夠有效控制內存和進程、能夠平滑重載PHP配置,比spawn-fcgi具備更多優勢,因此被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數便可開啓PHP-FPM,其它參數都是配置php的,具體選項含義能夠查看這裏

安裝前準備
centos下執行

yum -y install gcc automake autoconf libtool make yum -y install gcc gcc-c++ glibc yum -y install libmcrypt-devel mhash-devel libxslt-devel \ libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \ zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \ ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \ krb5 krb5-devel libidn libidn-devel openssl openssl-devel

新版php-fpm安裝(推薦安裝方式)

wget http://cn2.php.net/distributions/php-5.4.7.tar.gz tar zvxf php-5.4.7.tar.gz cd php-5.4.7 ./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt \ --enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath \ --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \ --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \ --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \ --with-gd --with-jpeg-dir make all install

舊版手動打補丁php-fpm安裝(舊版程序已經沒有了,你們新版的吧,這裏作個展現)
wget http://cn2.php.net/get/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
tar zvxf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure --prefix=/usr/local/php -with-config-file-path=/usr/local/php/etc\
-with-mysql=/usr/local/mysql\
-with-mysqli=/usr/local/mysql/bin/mysql_config -with-openssl -enable-fpm -enable-mbstring\
-with-freetype-dir -with-jpeg-dir -with-png-dir -with-zlib-dir -with-libxml-dir=/usr -enable-xml\
-with-mhash -with-mcrypt -enable-pcntl -enable-sockets  -with-bz2 -with-curl -with-curlwrappers\
-enable-mbregex -with-gd -enable-gd-native-ttf -enable-zip -enable-soap -with-iconv -enable-bcmath\
-enable-shmop -enable-sysvsem -enable-inline-optimization -with-ldap -with-ldap-sasl -enable-pdo\
-with-pdo-mysql
make all install

以上兩種方式均可以安裝php-fpm,安裝後內容放在/usr/local/php目錄下

以上就完成了php-fpm的安裝。

下面是對php-fpm運行用戶進行設置

cd /usr/local/php cp etc/php-fpm.conf.default etc/php-fpm.conf vi etc/php-fpm.conf

修改
user = www-data
group = www-data

若是www-data用戶不存在,那麼先添加www-data用戶
groupadd www-data
useradd -g www-data www-data

2、編譯安裝nginx

而後按照http://www.nginx.cn/install 安裝nginx

3、修改nginx配置文件以支持php-fpm

nginx安裝完成後,修改nginx配置文件爲,nginx.conf

其中server段增長以下配置,注意標紅內容配置,不然會出現No input file specified.錯誤

# 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 $document_root$fastcgi_script_name;
include fastcgi_params;
}

4、建立測試php文件

建立php文件

在/usr/local/nginx/html下建立index.php文件,輸入以下內容

<?php echo phpinfo(); ?>

5、啓動服務

啓動php-fpm和nginx

/usr/local/php/sbin/php-fpm #手動打補丁的啓動方式/usr/local/php/sbin/php-fpm start sudo /usr/local/nginx/nginx

php-fpm關閉重啓見文章結尾

6、瀏覽器訪問

訪問http://你的服務器ip/index.php,皆能夠見到php信息了。

安裝php-fpm時可能遇到的錯誤:

1. php configure時出錯

configure: error: XML configuration could not be found

apt-get install libxml2 libxml2-dev  (ubuntu下) yum -y install libxml2 libxml2-develcentos下)

2. Please reinstall the BZip2 distribution

wget http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz tar -zxvf bzip2-1.0.5.tar.gz cd bzip2-1.0.5 make make install

3. php的配置文件中有一行--with-mysql=/usr
安裝的時候提示:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore.

這是因爲安裝mysql時沒有安裝mysql頭文件,或者是路徑指定不正確,php找不到mysql的頭文件引發的錯誤提示。
解決方法。
(1.) 查看你的系統有沒有安裝mysql header
find / -name mysql.h
若是有。請指定--with-mysql=/跟你的正常路徑。
若是沒有。請看下一步。
(2.)redhat安裝
rpm -ivh MySQL-devel-4.1.12-1.i386.rpm
(3.)ubuntu安裝
apt-get install libmysqlclient15-dev
(4.)最後一步php的配置選項添加--with-mysql=/usr便可!

4.No input file specified.

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

5. 若是php configure時缺庫,能夠先安裝庫(ubuntu下)

sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev

6. mcrypt.h not found. Please reinstall libmcrypt

apt-get install libmcrypt-dev

或者

cd /usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install

7. php-fpm 5.4.7 如何關閉 重啓?

php 5.4.7 下的php-fpm 再也不支持 php-fpm 之前具備的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,須要使用信號控制:

master進程能夠理解如下信號

INT, TERM 馬上終止 QUIT 平滑終止 USR1 從新打開日誌文件 USR2 平滑重載全部worker進程並從新載入配置和二進制模塊

示例:

php-fpm 關閉:

kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

php-fpm 重啓:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

查看php-fpm進程數:

ps aux | grep -c php-fpm

8.命令行下執行php,提示找不到命令

-bash: /usr/bin/php: No such file or directory

vi /etc/profile

在文件底部增長一行配置
export PATH=/usr/local/php/bin:$PATH

保存退出

source /etc/profile

13.nginx屏蔽ip

採集和防止採集是一個經久不息的話題,一方面都想搞別人的東西,另外一方面不想本身的東西被別人搞走。

本文介紹如何利用nginx屏蔽ip來實現防止採集,固然也能夠經過iptable來實現。

1.查找要屏蔽的ip

awk '{print $1}' nginx.access.log |sort |uniq -c|sort -n

nginx.access.log 爲日誌文件,

會到以下結果,前面是ip的訪問次數,後面是ip,很明顯咱們須要把訪問次數多的ip而且不是蜘蛛的ip屏蔽掉,本例當中咱們屏蔽掉165.91.122.67

 ...   13610 202.112.113.192   95772 180.169.22.135  337418 219.220.141.2  558378 165.91.122.67

2.在nginx的安裝目錄下面,新建屏蔽ip文件,命名爲blockip.conf,之後新增長屏蔽ip只需編輯這個文件便可。 加入以下內容

deny 165.91.122.67; 

保存一下。

3.在nginx的配置文件nginx.conf中加入以下配置,能夠放到http, server, location, limit_except語句塊,須要注意相對路徑,本例當中nginx.conf,blocksip.conf在同一個目錄中。

include blockip.conf; 

 

4.重啓一下nginx的服務:/usr/local/nginx/nginx -s reload 就能夠生效了。

高級用法:

屏蔽ip的配置文件既能夠屏蔽單個ip,也能夠屏蔽ip段,或者只容許某個ip或者某個ip段訪問。

# 屏蔽單個ip訪問

deny IP; 

# 容許單個ip訪問

allow IP; 

# 屏蔽全部ip訪問

deny all; 

# 容許全部ip訪問

allow all; 

#屏蔽整個段即從123.0.0.1到123.255.255.254訪問的命令

deny 123.0.0.0/8

#屏蔽IP段即從123.45.0.1到123.45.255.254訪問的命令

deny 124.45.0.0/16

#屏蔽IP段即從123.45.6.1到123.45.6.254訪問的命令

deny 123.45.6.0/24

若是你想實現這樣的應用,除了幾個IP外,其餘所有拒絕,
那須要你在blockip.conf中這樣寫

allow 1.1.1.1;  allow 1.1.1.2; deny all; 

單獨網站屏蔽IP的方法,把include blocksip.conf; 放到網址對應的在server{}語句塊,全部網站屏蔽IP的方法,把include blocksip.conf; 放到http {}語句塊。

相關文章
相關標籤/搜索