nginx簡單配置及使用

最近感覺nginx各種火,大有取代apache的趨勢,於是學一學,虛擬機裏搭個服務器練一練。

學了一下午,略有心得,記下來大家一起分享。

安裝過程不做重點:nginx安裝最簡單,tomcat和jdk安裝加環境變量,php(fastcgi)安裝最麻煩,後文會給出部分說明。

 

概要

這篇文章將包括以下內容:

nginx查找虛擬主機原理的簡單介紹;

通過不同ip訪問同一臺服務器到達不同的網頁根目錄;

nginx與php(fastcgi)結合;

nginx與tomcat結合。

 

環境

虛擬機環境:

版本 VMware7.1.2 

硬盤 10GB

內存 1024MB

聯網方式 NAT

 

軟件版本:

nginx-0.7.69.tar.gz

jdk-7u1-linux-i586.tar.gz

apache-tomcat-6.0.32.tar.gz

libiconv-1.13.tar.gz

libmcrypt-2.5.8.tar.bz2

mhash-0.9.9.9.tar.bz2

mcrypt-2.6.8.tar.gz

php-5.2.17.tar.gz

php-5.2.17-fpm-0.5.14.diff.gz

 

 

1. nginx查找虛擬主機原理的簡單介紹

 

nginx可以基於server_name, port, ip來匹配虛擬主機。

通過server_name區分
以下三個服務器均監聽80端口,依靠HTTP請求裏的HOST來區分
server {
listen 80;
server_name nginx.org www.nginx.org;
...
}

server {
listen 80;
server_name nginx.net www.nginx.net;
...
}

server {
listen 80;
server_name nginx.com www.nginx.com;
...
}
如果HOST沒有對應的服務器或者沒有HOST,那麼交給default server。


配置一個default server:
server {
# 在0.8.21版之前應該使用default
listen 80 default_server;
server_name nginx.net www.nginx.net;
...
}

如果想要明確的禁止沒有HOST的HTTP請求,可以這樣定義一個服務器:
server {
listen 80;
# 0.8.48版之後,下邊這句server_name可以省略
server_name "";
return 444;
}


同時使用ip和名字來定義虛擬主機
server {
listen 192.168.1.1:80;
server_name nginx.org www.nginx.org;
...
}

server {
listen 192.168.1.1:80;
server_name nginx.net www.nginx.net;
...
}

server {
listen 192.168.1.2:80;
server_name nginx.com www.nginx.com;
...
}

默認服務器也可以有多個,根據ip:port區分,例如:
server {
listen 192.168.1.1:80;
server_name nginx.org www.nginx.org;
...
}

server {
listen 192.168.1.1:80 default_server;
server_name nginx.net www.nginx.net;
...
}

server {
listen 192.168.1.2:80 default_server;
server_name nginx.com www.nginx.com;
...
}

location指令的簡單使用
爲了講解location指令的使用,先給出一個簡單的php主機配置:

server {
    listen 80;
    server_name nginx.org www.nginx.org;
    root /data/www;

    location / {
        index index.html index.php;
    }

    location ~* \.(gif|jpg|png)$ {
        expires 30d;
    }

    location ~ \.php$ {
        fastcgi_pass localhost:9000;
        fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

location使用pcre的正則表達式引擎來匹配請求地址,並進行相應處理。
location \作爲特殊的規則,任何請求的uri都匹配他,但只有在不匹配其他location的正則表達式時纔會查看它裏邊的規則。
除此之外,其他的location匹配從上到下,有一個匹配符合之後會停止查找,nginx使用該location定義的規則進行處理。
location只會檢查請求的uri部分,不會檢查參數(請求中"?"及其之後的部分)。
例如:
請求“/logo.gif” 匹配了"\"和"\.(gif|jpg|png)$",它使用了第二個的規則處理,然後在“root /data/www”影響下,映射到/data/www/logo.gif。此文件被返回到client。
請求“/index.php”匹配了“\”和“\.(php)$”,然後被傳遞給fastcgi localhost:9000。fastcgi_param指令將SCRIPT_FILENAME設置爲“/data/www/index.php”
請求“/about.html”只匹配“\”,於是他被映射到/data/www/about.html,文件被髮回給client。
請求“/”只匹配“\”,因爲是目錄,找到location \的index指令,映射爲“/index.php”。然後再次匹配,依據“\.(php)$”的指令交給fastcgi。

 

 

2.  通過不同ip訪問同一臺服務器到達不同的網頁根目錄

在大致瞭解了nginx查找server的原理後,我們就可以試試通過不同ip訪問不同網站根目錄了。

首先,由於只有一個網卡,我只能在一個網卡上設置幾個ip alias,來模擬多ip。

 

目標:

192.168.159.133:80訪問到/home/tricky1997/nginx/data/下的網頁,並且爲默認server;

192.168.159.134:80訪問到/home/tricky1997/nginx/data1/下的網頁;

192.168.159.135:80訪問到/home/tricky1997/nginx/data2/下的網頁;

404 500 502 503 504等錯誤的地址的訪問,均會訪問到/home/tricky1997/nginx/error下的50x.html。

 

配置文件路徑爲/home/tricky1997/nginx/nginx.conf,使用/usr/sbin/nginx/sbin/nginx -c /home/tricky1997/nginx/nginx.conf啓動。

配置文件內容如下:

  1. user  tricky1997;  
  2. worker_processes  1;  
  3.   
  4. error_log  off;  
  5. pid        /home/tricky1997/nginx/logs/nginx.pid;  
  6.   
  7. events {  
  8.     use epoll;  
  9.     worker_connections  10;  
  10. }  
  11.   
  12. http {  
  13.     include       mime.types;  
  14.     default_type  application/octet-stream;  
  15.   
  16.     access_log      off;  
  17.     sendfile        on;  
  18.     keepalive_timeout  65;  
  19.    
  20.     server_names_hash_bucket_size 128;  
  21.     client_header_buffer_size     32k;  
  22.     large_client_header_buffers  4 32k;  
  23.     tcp_nodelay on;  
  24.   
  25.           
  26.     server {  
  27.         listen       192.168.159.133:80;  
  28.         server_name  192.168.159.133 default;  
  29.         root  /home/tricky1997/nginx/data;  
  30.           
  31.         location \ {  
  32.             index  index.html index.htm;  
  33.         }  
  34.   
  35.         error_page   404 500 502 503 504  /50x.html;  
  36.         location /50x.html{  
  37.             root   /home/tricky1997/nginx/error;  
  38.         }  
  39.     }  
  40.   
  41.     server {  
  42.         listen       192.168.159.134:80;  
  43.         server_name  192.168.159.134;  
  44.         index index.html index.htm;  
  45.           
  46.         root  /home/tricky1997/nginx/data1;  
  47.           
  48.         location \ {  
  49.             index  index.html index.htm;  
  50.         }  
  51.   
  52.         error_page   404 500 502 503 504  /50x.html;  
  53.         location /50x.html{  
  54.             root   /home/tricky1997/nginx/error;  
  55.         }  
  56.     }  
  57.   
  58.     server {  
  59.         listen       192.168.159.135:80;  
  60.         server_name  192.168.159.135;  
  61.         root  /home/tricky1997/nginx/data2;  
  62.           
  63.         location \ {  
  64.             index  index.html index.htm;  
  65.         }  
  66.   
  67.         error_page   404 500 502 503 504  /50x.html;  
  68.         location /50x.html{  
  69.             root   /home/tricky1997/nginx/error;  
  70.         }  
  71.     }  
  72.   
  73. }  

 

3. nginx與tomcat結合

 

目標:

通過nginx的反向代理tomcat,打開jsp網頁。

jsp網頁所在路徑爲/home/tricky1997/nginx/jsp/ROOT/index.jsp。通過http://192.168.159.133/ROOT/index.jsp訪問。

 

安裝完tomcat和jdk後,在/etc/profile最後添加兩行環境變量的設置:

JAVA_HOME="/usr/local/jdk"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH="$PATH:$JAVA_HOME/bin"
CATALINA_HOME="/usr/local/tomcat"
export JAVA_HOME CATALINA_HOME

具體路徑根據安裝時的選擇而變。保存後,source /etc/profile更新。

修改tomcat的xml配置文件,將webroot設置爲/home/tricky1997/nginx。

啓動tomcat。

將測試用的jsp網頁放在/home/tricky1997/nginx/jsp文件夾下,啓動nginx。

 

配置文件路徑/home/tricky1997/nginx/jsp_nginx.conf,內容如下:

  1. user  tricky1997 tricky1997;  
  2. worker_processes  1;  
  3.   
  4. error_log  off;  
  5.   
  6. pid        /home/tricky1997/nginx/logs/nginx.pid;  
  7.   
  8. events {  
  9.     use epoll;  
  10.     worker_connections  10;  
  11. }  
  12.   
  13.   
  14. http {  
  15.     include       mime.types;  
  16.     default_type  application/octet-stream;  
  17.   
  18.     access_log      off;  
  19.     sendfile        on;  
  20.   
  21.     keepalive_timeout  65;  
  22.     server_names_hash_bucket_size 128;  
  23.     client_header_buffer_size     32k;  
  24.     large_client_header_buffers  4 32k;  
  25.     tcp_nodelay on;  
  26.   
  27.     proxy_connect_timeout  5;  
  28.     proxy_read_timeout     60;      
  29.     proxy_send_timeout     60;  
  30.     proxy_buffer_size     16k;  
  31.     proxy_buffers       4 64k;  
  32.     proxy_busy_buffers_size    128k;  
  33.     proxy_temp_file_write_size 128k;  
  34.   
  35.     upstream tomcat_server{  
  36.         server 127.0.0.1:8080;  
  37.     }  
  38.   
  39.     server {  
  40.         listen       192.168.159.133:80;  
  41.         server_name  192.168.159.133 default;  
  42.         index index.html index.htm index.jsp index.do;  
  43.   
  44.         root  /home/tricky1997/nginx/webapp;  
  45.           
  46.         if (-d $request_filename)  
  47.         {  
  48.             rewrite ^/(.*)([^/])$  http://$host/$1$2/ permanent;  
  49.         }  
  50.   
  51.         location ~ \.(jsp|jspx|do)?$ {  
  52.             proxy_set_header Host  $host;  
  53.             proxy_set_header X-Forwarded_For  $remote_addr;  
  54.             proxy_pass   http://tomcat_server;  
  55.         }  
  56.   
  57.         error_page   404 500 502 503 504  /50x.html;  
  58.         location /50x.html{  
  59.             root   /home/tricky1997/nginx/error;  
  60.         }  
  61.   
  62.     }  
  63.   
  64. }      


 


4. nginx與php(fastcgi)結合

 

目標:

通過nginx反向代理到php(fastcgi)訪問php頁面。

php網頁所在路徑爲/home/tricky1997/nginx/php/index.php。通過http://192.168.159.133/index.php訪問。

 

之前沒接觸過php,完全不會php開發,因此php的搭建花了很長時間。下載安裝配置php(fastcgi)就花了將近一個小時。這裏不再介紹,建議另找資料。

安裝完各種包之後,配置php.ini,配置php-fpm.conf。

通過/usr/local/webserver/php/sbin/php-fpm start啓動php-cgi進程。

然後啓動nginx。

 

配置文件路徑爲/home/tricky1997/nginx/php_nginx.conf,內容如下:

  1. user  tricky1997;  
  2. worker_processes  1;  
  3.   
  4. error_log  off;  
  5. pid        /home/tricky1997/nginx/logs/php_nginx.pid;  
  6.   
  7. events {  
  8.     use epoll;  
  9.     worker_connections  10;  
  10. }  
  11.   
  12. http {  
  13.     include       mime.types;  
  14.     default_type  application/octet-stream;  
  15.   
  16.     access_log      off;  
  17.       
  18.     server_names_hash_bucket_size 128;  
  19.     client_header_buffer_size     32k;  
  20.     large_client_header_buffers  4 32k;  
  21.     client_max_body_size 8m;  
  22.     tcp_nodelay on;  
  23.     sendfile        on;  
  24.     keepalive_timeout  65;  
  25.   
  26.     fastcgi_connect_timeout 300;  
  27.     fastcgi_send_timeout 300;  
  28.     fastcgi_read_timeout 300;  
  29.     fastcgi_buffer_size 64k;  
  30.     fastcgi_buffers 4 64k;  
  31.     fastcgi_busy_buffers_size 128k;  
  32.     fastcgi_temp_file_write_size 128k;  
  33.   
  34.     gzip  on;  
  35.     gzip_buffers 4 16k;  
  36.     gzip_min_length 1k;  
  37.     gzip_http_version 1.0;  
  38.     gzip_comp_level 2;  
  39.     gzip_types text/plain application/x-javascript text/css application/xml;  
  40.     gzip_vary on;  
  41.   
  42.     server {  
  43.         listen       192.168.159.133:80;  
  44.         server_name  192.168.159.133 default;  
  45.         index index.html index.htm index.php;  
  46.   
  47.         root  /home/tricky1997/nginx/php;  
  48.   
  49.         location ~ .*\.(php|php5)?$ {  
  50.             fastcgi_pass 127.0.0.1:9000;  
  51.             fastcgi_index index.php;  
  52.             include /usr/local/nginx/conf/fcgi.conf;  
  53.         }          
  54.   
  55.   
  56.         error_page   404 500 502 503 504  /50x.html;  
  57.         location /50x.html{  
  58.             root   /home/tricky1997/nginx/error;  
  59.         }  
  60.     }  
  61.   
  62. }  

 

 

總結

nginx安裝方便,規則靈活。

可以靈活實現虛擬主機、反向代理、負載平衡、靜態動態服務器分離、URL重寫。

容易與jsp,php,perl和緩存服務器結合。

輪詢機制使用libevent,在linux上使用epoll,freebsd上使用kqueue,性能速度不弱於apache。

發起較晚,但成長迅速,很多著名網站均使用nginx做服務器,如Rambler,新浪博客,搜狐博客,迅雷等。前景看好。

發佈了28 篇原創文章 · 獲贊 11 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章