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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章