最近感觉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,新浪博客,搜狐博客,迅雷等。前景看好。