標籤:
Nginx 是一個輕量級的高效能 Http WebServer,以事件驅動方式編寫,因此相比 Apache 而言,Nginx 更加穩定、效能更好,而且配置簡單,資源佔用較低。
1. 安裝 Nginx
從 v0.7.52 開始,Nginx 開始發布 Windows 版本的 Nginx,你可以在其官方網站上面下載:http://nginx.net
下載後直接解壓即可,這裡解壓縮到c:\nginx目錄。
2. 啟動Nginx
命令列進入c:\nginx目錄,運行nginx.exe,啟動控制台視窗。預設啟用80連接埠。用過Tomcat的人都希望能在控制台看到開機記錄,nginx的日誌卻不得不查看logs目錄下的相應log檔案。
3. 訪問歡迎html頁
在瀏覽器中訪問http://localhost,可以看到預設的歡迎頁面.
4. 停止Nginx
Ctrl+C沒反應。於是關閉控制台視窗。可是再訪問http://localhost依然有效。查看進程,發現nginx根本沒有被關閉。因此如果想徹底關閉nginx,應該是
Command代碼
- nginx -s stop
請參考官方文檔nginx/Windows usage
或者使用windows的taskkill命令(推薦用下面的):
Command代碼
- taskkill /F /IM nginx.exe > nul
5. Ngnix常用配置
Nginx的所有配置都預設使用conf/nginx.conf檔案,其地位相當於apache的httpd.conf檔案 。當運行nginx.exe暗含運行了nginx -c 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;
- }
- }
- }
小提示:
運行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
顯然,最經常用的memcache, rewrite模組都沒在其中,因此該win32編譯版本僅能供基本開發測試使用,對於產品平台,應該重新編譯自己想要的win32版本,或者在linux下使用更方便。
Nginx - Windows下Nginx基本安裝和配置