nginx [engine x] 是由 Igor Sysoev开发的一个HTTP 服务器和mail 代理服务器软件.虽然刚刚发布两年多, Nginx 因其稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。Nginx 超越Apache的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中有新浪、网易、腾讯等门户网站,六间房、酷6等视频分享网站,Discuz!、水木社区等知名论坛,豆瓣、YUPOO等新兴Web 2.0网站。Nginx 在国内的应用正在不断发展壮大!新近发现Nginx 应用在国内越发火热了,很多网站都开始转向Nginx 了。
一,首先下载nginx 0.7.52的windows版本,下载地址:http://sysoev.ru/nginx/nginx-0.7.52.zip,并到PHP 官方下载php。
然后,解压nginx-0.7.52.zip到C盘的根目录,并将目录名改为nginx。执行下列操作
1, cd nginx
2, start nginx
这样,nginx就启动了。打开浏览器,输入http://127.0.0.1/ 就可以看到nginx的欢迎页面了。nginx的其他命令
nginx -s stop //停止nginx
nginx -s reload //重新加载配置文件
nginx -s quit //退出nginx
#网站的图片较多,更改较少,将它们在浏览器本地缓存15天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 15d;
}
#网站会加载很多JS、CSS,将它们在浏览器本地缓存1天
location ~ .*\.(js|css)?$
{
expires 1d;
}
location /(WEB-INF)/ {
deny all;
}
#设定本虚拟主机的访问日志
access_log /host/nginx/logs/down/access.log access;
#日志的路径,每个虚拟机一个,不能相同
server_name_in_redirect off;
实验环境
win7+ubuntu(vmware)
win7 tomcat(ip and por:192.168.0.108:8080)
linux tomcat(ip and por:192.168.110.129:8080)
在ubuntu中安装nginx(sudo apt-get install nginx),默认安装在etc/nginx
在两台tomcat的webapps下的ROOT中,为了表示出访问的是那一台tomcat,替换index.html以及index.jsp,其中分别写入linux和windows。
nginx的启动关闭和测试命令如下:
- 关闭nginx
- zhengx@zhengx-virtual-machine:~$ sudo pkill -9 nginx
- 测试nginx是否成功配置
- zhengx@zhengx-virtual-machine:~$ sudo nginx -t
- nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
- nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
- 开启nginx
- zhengx@zhengx-virtual-machine:~$ sudo nginx
- nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
- zhengx@zhengx-virtual-machine:~$
其中nginx.conf的配置文件如下所示
- user www-data;
- worker_processes 4;
- pid /var/run/nginx.pid;
- events {
- worker_connections 768;
- # multi_accept on;
- }
- http {
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 65;
- types_hash_max_size 1024;
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- access_log /var/log/nginx/access.log;
- error_log /var/log/nginx/error.log;
- gzip on;
- upstream localhost {
- #win7的tomcat访问url
- server 192.168.0.108:8080;
- #linux的tomcat访问url
- server 192.168.110.129:8080;
- }
- server {
- listen 80;
- server_name localhost;
- charset utf-8;
- location / {
- root html;
- index index.html index.htm;
- #跳转到win7上的tomcat
- proxy_pass http://192.168.0.108:8080;
- proxy_set_header X-Real-IP $remote_addr;
- client_max_body_size 100m;
- }
- location ~ ^/(WEB-INF)/ {
- deny all;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- include /etc/nginx/server.conf;
- include /etc/nginx/conf.d/*.conf;
- include /etc/nginx/sites-enabled/*;
- }
为了便于维护把nginx.conf拆分一部分代码出来分别是upstream.conf和location.conf
nginx.conf的配置文件如下:
- user www-data;
- worker_processes 4;
- pid /var/run/nginx.pid;
- events {
- worker_connections 768;
- # multi_accept on;
- }
- http {
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 65;
- types_hash_max_size 1024;
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- access_log /var/log/nginx/access.log;
- error_log /var/log/nginx/error.log;
- gzip on;
- include /etc/nginx/upstream.conf;
- server {
- listen 80;
- server_name localhost;
- charset utf-8;
- include /etc/nginx/location.conf;
- }
- include /etc/nginx/conf.d/*.conf;
- include /etc/nginx/sites-enabled/*;
- }
upstream.conf的配置文件如下:
- upstream localhost {
- server 192.168.0.108:8080;
- server 192.168.110.129:8080;
- }
location.conf的配置文件如下:
- location / {
- root html;
- index index.html index.htm;
- #去找域名为localhost的upstream
- proxy_pass http://localhost;
- proxy_set_header X-Real-IP $remote_addr;
- client_max_body_size 100m;
- }
- location ~ ^/(WEB-INF)/ {
- deny all;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }