最近感覺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啓動。
配置文件內容如下:
- user tricky1997;
- worker_processes 1;
- error_log off;
- pid /home/tricky1997/nginx/logs/nginx.pid;
- events {
- use epoll;
- worker_connections 10;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- access_log off;
- sendfile on;
- keepalive_timeout 65;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- tcp_nodelay on;
- server {
- listen 192.168.159.133:80;
- server_name 192.168.159.133 default;
- root /home/tricky1997/nginx/data;
- location \ {
- index index.html index.htm;
- }
- error_page 404 500 502 503 504 /50x.html;
- location /50x.html{
- root /home/tricky1997/nginx/error;
- }
- }
- server {
- listen 192.168.159.134:80;
- server_name 192.168.159.134;
- index index.html index.htm;
- root /home/tricky1997/nginx/data1;
- location \ {
- index index.html index.htm;
- }
- error_page 404 500 502 503 504 /50x.html;
- location /50x.html{
- root /home/tricky1997/nginx/error;
- }
- }
- server {
- listen 192.168.159.135:80;
- server_name 192.168.159.135;
- root /home/tricky1997/nginx/data2;
- location \ {
- index index.html index.htm;
- }
- error_page 404 500 502 503 504 /50x.html;
- location /50x.html{
- root /home/tricky1997/nginx/error;
- }
- }
- }
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,內容如下:
- user tricky1997 tricky1997;
- worker_processes 1;
- error_log off;
- pid /home/tricky1997/nginx/logs/nginx.pid;
- events {
- use epoll;
- worker_connections 10;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- access_log off;
- sendfile on;
- keepalive_timeout 65;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- tcp_nodelay on;
- proxy_connect_timeout 5;
- proxy_read_timeout 60;
- proxy_send_timeout 60;
- proxy_buffer_size 16k;
- proxy_buffers 4 64k;
- proxy_busy_buffers_size 128k;
- proxy_temp_file_write_size 128k;
- upstream tomcat_server{
- server 127.0.0.1:8080;
- }
- server {
- listen 192.168.159.133:80;
- server_name 192.168.159.133 default;
- index index.html index.htm index.jsp index.do;
- root /home/tricky1997/nginx/webapp;
- if (-d $request_filename)
- {
- rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
- }
- location ~ \.(jsp|jspx|do)?$ {
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded_For $remote_addr;
- proxy_pass http://tomcat_server;
- }
- error_page 404 500 502 503 504 /50x.html;
- location /50x.html{
- root /home/tricky1997/nginx/error;
- }
- }
- }
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,內容如下:
- user tricky1997;
- worker_processes 1;
- error_log off;
- pid /home/tricky1997/nginx/logs/php_nginx.pid;
- events {
- use epoll;
- worker_connections 10;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- access_log off;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 8m;
- tcp_nodelay on;
- sendfile on;
- keepalive_timeout 65;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 64k;
- fastcgi_buffers 4 64k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- gzip on;
- gzip_buffers 4 16k;
- gzip_min_length 1k;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- server {
- listen 192.168.159.133:80;
- server_name 192.168.159.133 default;
- index index.html index.htm index.php;
- root /home/tricky1997/nginx/php;
- location ~ .*\.(php|php5)?$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include /usr/local/nginx/conf/fcgi.conf;
- }
- error_page 404 500 502 503 504 /50x.html;
- location /50x.html{
- root /home/tricky1997/nginx/error;
- }
- }
- }
總結
nginx安裝方便,規則靈活。
可以靈活實現虛擬主機、反向代理、負載平衡、靜態動態服務器分離、URL重寫。
容易與jsp,php,perl和緩存服務器結合。
輪詢機制使用libevent,在linux上使用epoll,freebsd上使用kqueue,性能速度不弱於apache。
發起較晚,但成長迅速,很多著名網站均使用nginx做服務器,如Rambler,新浪博客,搜狐博客,迅雷等。前景看好。