1. 程式人生 > >Nginx 教程一:在windows環境下的安裝、配置、使用以及設定為windows服務自啟動

Nginx 教程一:在windows環境下的安裝、配置、使用以及設定為windows服務自啟動

目前國內各大入口網站已經部署了Nginx,如新浪、網易、騰訊等;國內幾個重要的視訊分享網站也部署了Nginx,如六房間、酷6等。新近發現Nginx 技術在國內日趨火熱,越來越多的網站開始部署Nginx。

    相比apeach、iis,nginx以輕量級、高效能、穩定、配置簡單、資源佔用少等優勢廣受歡迎。

下載地址:

啟動

  解壓至c:\nginx,執行nginx.exe(即nginx -c conf\nginx.conf),預設使用80埠,日誌見資料夾C:\nginx\logs; 若nginx.exe啟動一閃而過,則需要修改nginx.conf中的預設埠

使用

關閉

  nginx -s stop 或taskkill /F /IM nginx.exe > nul 

常用配置

   C:\nginx\conf\nginx.conf,使用自己定義的conf檔案如my.conf,命令為nginx -c conf\my.conf

  常用配置如下: 
  Nginx.conf程式碼 
  http { 
   server { 
   #1.偵聽80埠 
   listen 80; 
   location / { 
   # 2. 預設主頁目錄在nginx安裝目錄的html子目錄。 
   root html; 
   index index.html index.htm; 
   # 3. 沒有索引頁時,羅列檔案和子目錄 
   autoindex on; 
   autoindex_exact_size on; 
   autoindex_localtime on; 
   } 
   # 4.指定虛擬目錄 
   location /tshirt { 
   alias D:\programs\Apache2\htdocs\tshirt; 
   index index.html index.htm; 
   } 
   } 
   # 5.虛擬主機www.emb.info配置 
   server { 
   listen 80; 
   server_name www.emb.info; 
   access_log emb.info/logs/access.log; 
   location / { 
   index index.html; 
   root emb.info/htdocs; 
   } 
   } 
  } 
  
  http {
   server {
   #1.偵聽80埠 
   listen 80; 
   location / {
   # 2. 預設主頁目錄在nginx安裝目錄的html子目錄。
   root html;
   index index.html index.htm;
   # 3. 沒有索引頁時,羅列檔案和子目錄
   autoindex on;
   autoindex_exact_size on;
   autoindex_localtime on;
   }
   # 4.指定虛擬目錄
   location /tshirt {
   alias D:\programs\Apache2\htdocs\tshirt;
   index index.html index.htm;
   }
   }
   # 5.虛擬主機www.emb.info配置
   server {
   listen 80;
   server_name www.emb.info;
   access_log emb.info/logs/access.log;
   location / {
   index index.html;
   root emb.info/htdocs;
   }
   }
  }
  
  小提示: 
  執行nginx -V可以檢視該Win32平臺編譯版支援哪些模組。我這裡的結果為: 
  Log程式碼 
  nginx version: nginx/0.7.65 
  TLS SNI support enabled 
  configure arguments: 
  --builddir=objs.msvc8 
  --crossbuild=win32 
  --with-debug --prefix= 
  --conf-path=conf/nginx.conf 
  --pid-path=logs/nginx.pid 
  --http-log-path=logs/access.log 
  --error-log-path=logs/error.log 
  --sbin-path=nginx.exe 
  --http-client-body-temp-path=temp/client_body_temp 
  --http-proxy-temp-path=temp/proxy_temp 
  --http-fastcgi-temp-path=temp/fastcgi_temp 
  --with-cc-opt=-DFD_SETSIZE=1024 
  --with-pcre=objs.msvc8/lib/pcre-7.9 
  --with-openssl=objs.msvc8/lib/openssl-0.9.8k 
  --with-openssl-opt=enable-tlsext 
  --with-zlib=objs.msvc8/lib/zlib-1.2.3 
  --with-select_module 
  --with-http_ssl_module 
  --with-http_realip_module 
  --with-http_addition_module 
  --with-http_sub_module 
  --with-http_dav_module 
  --with-http_stub_status_module 
  --with-http_flv_module 
  --with-http_gzip_static_module 
  --with-http_random_index_module 
  --with-http_secure_link_module 
  --with-mail 
  --with-mail_ssl_module 
  --with-ipv6 
  
  nginx version: nginx/0.7.65
  TLS SNI support enabled
  configure arguments: 
  --builddir=objs.msvc8 
  --crossbuild=win32 
  --with-debug --prefix= 
  --conf-path=conf/nginx.conf 
  --pid-path=logs/nginx.pid 
  --http-log-path=logs/access.log 
  --error-log-path=logs/error.log 
  --sbin-path=nginx.exe 
  --http-client-body-temp-path=temp/client_body_temp 
  --http-proxy-temp-path=temp/proxy_temp 
  --http-fastcgi-temp-path=temp/fastcgi_temp 
  --with-cc-opt=-DFD_SETSIZE=1024 
  --with-pcre=objs.msvc8/lib/pcre-7.9 
  --with-openssl=objs.msvc8/lib/openssl-0.9.8k 
  --with-openssl-opt=enable-tlsext 
  --with-zlib=objs.msvc8/lib/zlib-1.2.3 
  --with-select_module 
  --with-http_ssl_module 
  --with-http_realip_module 
  --with-http_addition_module 
  --with-http_sub_module 
  --with-http_dav_module 
  --with-http_stub_status_module 
  --with-http_flv_module 
  --with-http_gzip_static_module 
  --with-http_random_index_module 
  --with-http_secure_link_module 
  --with-mail 
  --with-mail_ssl_module 
  --with-ipv6
  
  顯然,最經常用的memcache, rewrite模組都沒在其中,因此該win32編譯版本僅能供基本開發測試使用,對於產品平臺,應該重新編譯自己想要的win32版本,或者在linux下使用更方便。

簡單原理

配置檔案基本結構就是這樣子,由若干指令(directives)構成。指令分為簡單指令(siple directives)和塊指令(block directives)。

簡單指令由指令名和引數構成,指令名和引數以空格分隔,每條指令以分號結尾。例如 

user nginx; 

這就是一條簡單指令,表示以 nginx 這個使用者身份執行 nginx 工作程序。指令名為 user ,引數為 nginx,最後分號結束。

塊指令由指令名和若干由花括號{}包圍起來的一組指令組成。例如

events {

    worker_connections 1024;

}

就是一個塊指令,指令名為 events,後面緊跟 {} 包圍起來的一組指令。

如果一個塊指令內有其他指令,那麼這個塊指令也成為上下文(context),不在任何上下文中的指令被認為是在主上下文中(main context)。例如 events 和 http 位於主上下文中,server 位於 http 上下文中,location 則位於 server 上下文中。

以井號#開頭的行是註釋行,不起作用。

檢視nginx程序

  tasklist /fi "imagename eq nginx.exe",如下顯示:
映像名稱                       PID 會話名              會話#       記憶體使用
========================= ======== ================ =========== ============
nginx.exe                     8944 Console                    1      5,128 K
nginx.exe                     6712 Console                    1      5,556 K

nginx常用命令

nginx -s stop 強制關閉 
nginx -s quit 安全關閉 
nginx -s reload 改變配置檔案的時候,重啟nginx工作程序,來時配置檔案生效 
nginx -s reopen 開啟日誌檔案

nginx設定為windows自啟動

每次手動啟動和關閉nginx.exe比較麻煩,因此可以在windows系統下將nginx作為系統服務啟動。

a. 準備工作

下載安裝nginx,並記住安裝目錄 官網下載

下載winsw,下載地址 (http://www.cr173.com/soft/101797.html)

b. winsw設定

將winsw可執行程式複製到nginx安裝目錄下,並重命名為nginx-service

新建名為nginx-service.xml的檔案(注:檔名必須與可執行檔名相同)

並編輯如下,其中name為 服務名,executable為可執行程式路徑,logpath為程式執行日誌路徑

  1. <service>
  2.  <id>nginx</id>
  3.   <name>nginx</name>
  4.   <description>nginx</description>
  5.   <executable>E:\phpStudy\nginx\nginx.exe</executable>
  6.   <logpath>E:\phpStudy\nginx\</logpath>
  7.   <logmode>roll</logmode>
  8.   <depend></depend>
  9.   <startargument>-p E:\phpStudy\nginx</startargument>
  10.   <stopargument>-p E:\phpStudy\nginx -s stop</stopargument>
  11. </service>

如下:

c. 安裝服務

在nginx安裝目錄下執行cmd(快捷方式:shift + 滑鼠右鍵),執行命令:nginx-service.exe install

注:nginx-service.exe uninstall命令可刪除對應的系統服務

nginx-service.exestop命令可停止對應的系統服務

nginx-service.exe start命令可啟動對應的系統服務

d. 檢視服務是否安裝成功

計算機管理  -> 服務

如服務為未執行狀態,可在此啟動服務,或設定為自動啟動

注:若服務安裝成功,可在cmd(管理員身份)中對服務進行如下操作

啟動nginx :net start nginx

停止nginx:net stop nginx

e. 驗證nginx是否正常執行

在瀏覽器中開啟網址http://localhost

原文地址:http://www.cnblogs.com/chuncn/archive/2011/10/14/2212291.html