windows下nginx安裝、配置與使用

     Nginx 是一款高性能的,輕量級的HTTP Web 服務器和反向代理服務器及電子郵件 IMAP/POP3/SMTP 代理服務器。Nginx 是由俄羅斯的程序設計師 Igor Sysoev 所開發,爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,它已經在該站點運行超過四年多時間了,Igor 將源代碼以類BSD許可證的形式發佈。 Nginx 發佈四年來,Nginx 已經因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名了Nginx 超越Apache的高性能和穩定性,使得國內使用 Nginx 作爲 Web 服務器的網站也越來越多。目前國內各大門戶網站已經部署了Nginx,如新浪、網易、騰訊等;國內幾個重要的視頻分享網站也部署了Nginx,如六房間、酷6等。新近發現Nginx 技術在國內日趨火熱,越來越多的網站開始部署Nginx。相比apeach、iis,nginx以輕量級、高性能、穩定、配置簡單、資源佔用少等優勢廣受歡迎。

1)下載地址:

  http://nginx.org

2)啓動

  解壓nginx-1.8.1.zip至c:\nginx-1.8.1,運行目錄下的 

nginx.exe(即nginx -c conf\nginx.conf) 

或者

cd  c:\nginx-1.8.1\

start nginx

    默認使用80端口,日誌見文件夾C:\nginx-1.8.1\logs

3)使用

 打開任務管理器,查看 nginx.exe 進程,有二個進程會顯示,佔用系統資源,那是相當的少。然後再打開瀏覽器,輸入 http://127.0.0.1/  就可以看到nginx的歡迎頁面了

4)關閉

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

     nginx -s reload       // 重新加載配置文件

     nginx -s quit          // 退出nginx
5)常用配置

   C:\nginx-1.8.1\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 
  
  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下使用更方便。

6)查看nginx進程

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

7)nginx常用命令

start nginx 啓動nginx

nginx -s stop 強制關閉 
nginx -s quit 安全關閉 
nginx -s reload 改變配置文件的時候,重啓nginx工作進程,來時配置文件生效 
nginx -s reopen 打開日誌文件

8)其它
  可以通過配置文件開啓多個nginx工作進程,但同時只有其中一個nginx工作進程在工作,其他的阻塞等待。
  一個nginx工作進程最多同時可以處理1024個連接。
  nginx中需要共享內存的cache或者模塊無法在windows下正常使用。
  不過,nginx官方正在改進,將來nginx會以服務的方式運行,使用 I/O completion ports代替select方法,使多個工作進程能併發工作。
  要使用nginx配合php-cgi使用,需要修改環境變量,否則,php-cgi運行一定次數就推出,需要重啓,設置PHP_FCGI_MAX_REQUESTS這個變量爲0即可。

  以上在win7上通過。 

 8)nginx以windows服務形式啓動

  1.下載微軟兩個工具:

    instsrv.exe srvay.exe

  2.執行命令:

    instsrv Nginxc:/nginx/srvany.exe

  3.配置Nginx的運行參數

  可以直接將配置導入到註冊表

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/NGINX/Parameters]
"Application"="C://nginx//nginx.exe"
"AppParameters"=""
"AppDirectory"="C://nginx//"

  注意:windows 下的Nginx 內置的module 很多沒有,用Nginx -V 命令查看。

9)Nginx下部署mono+asp.net環境

  1、從Mono for Windows中提取FastCGI-Mono-Server

  2、Nginx nginx.conf 的配置: 

worker_processes  1;
error_log  logs/error-debug.log info;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type   text/plain;
    sendfile        on;

    keepalive_timeout  65;
    index  index.html index.htm;

    server {
        listen       80;
        server_name yourdomain.com;
        index index.aspx default.aspx;

        location / {
          root   D:\www/yourwebapp;

          fastcgi_pass   127.0.0.1:8000;
          fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
          include       fastcgi_params;
       }

    }
}

   將上面的 FastCGI-Mono-Server 提取出來,所有文件全部註冊到 GAC(否則 Web 應用會找不到他們,當然你也可以直接放到 webapp/bin),然後解壓到某個文件夾,這裏假設爲 D:/FastCGI-Mono-Server。

  之後我們就可以按下列命令運行 FastCGI:
  fastcgi-mono-server2 /socket=tcp:127.0.0.1:8000 /root="D:\www\yourwebapp" /applications=yourdomain.com:/:. /multiplex=True

  最後執行運行 Nginx 服務器,我們的 ASP.Net 程序就能脫離 IIS。


附nginx.conf

#user  nobody;
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  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;
	access_log off;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

	gzip on;
	gzip_min_length 1k;
	gzip_buffers 16 64k;
	gzip_http_version 1.1;
	gzip_comp_level 6;
	gzip_types text/plain application/x-javascript text/css application/xml;
	gzip_vary on;

	upstream myChat{
		server 127.0.0.1:8081;
		server 127.0.0.1:8090;
	}
	
	upstream myServer{
		server 127.0.0.1:8080;
		server 127.0.0.1:8086;
		server 127.0.0.1:8087;
	}
	
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://127.0.0.1:8081;
			proxy_set_header Host $host:$server_port;
        }
		#location / {
        #    proxy_pass http://myChat;
		#	proxy_set_header Host $host:$server_port;
        #}
		
		#location / {
        #    proxy_pass http://127.0.0.1:8080;
		#	proxy_set_header Host $host:$server_port;
        #}
		
		location /zycapwxsehr/ {
			proxy_pass http://myServer;
			proxy_set_header Host $host:$server_port;
		}
		
		location /hgcp-web-sehrandroid/ {
			proxy_pass http://127.0.0.1:8082;
			proxy_set_header Host $host:$server_port;
		}
		
		location /hgcp-web-sehrios/ {
			proxy_pass http://127.0.0.1:8083;
			proxy_set_header Host $host:$server_port;
		}
		
		location /web-xiamen-sc/ {
			proxy_pass http://127.0.0.1:8084;
			proxy_set_header Host $host:$server_port;
		}
		
		location /web-xiamen-sc-wechat/ {
			proxy_pass http://127.0.0.1:8085;
			proxy_set_header Host $host:$server_port;
		}
		
		location /zycapwxsehrtest/ {
			proxy_pass http://127.0.0.1:8088;
			proxy_set_header Host $host:$server_port;
		}

		location /web-xiamen-sc-ij/ {
			proxy_pass http://127.0.0.1:8089;
			proxy_set_header Host $host:$server_port;
		}
        #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;
        }

    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章