安装nginx1.2.0
下载nginx源码包
http://nginx.org/download/nginx-1.2.0.tar.gz
安装pcre支持
yum install pcre-devel
安装nginx
cd /opt/
tar zxvf nginx-1.2.0.tar.gz
cd nginx-1.2.0
./configure --with-http_stub_status_module --prefix=/usr/local/nginx
make && make install
安装完成
cd /usr/local/nginx/sbin/
./nginx
启动 nginx
lsof -i:80
防火墙添加80端口 重启防火墙
访问 http://192.168.1.1
welcome to nginx !
实现简单负载
修改 /usr/local/nginx/conf/nginx.conf
修改内容如下:
http {
include mime.types;
default_type application/octet-stream;
upstream test{
server 192.168.1.2:80 weight=3 max_fails=3 fail_timeout=20s;
server 192.168.1.3:80 weight=3 max_fails=4 fail_timeout=20s;
server 192.168.1.4:80 weight=3 max_fails=1 fail_timeout=20s;
}
server {
listen 80;
server_name www.test.com 192.168.1.1;
index index.html index.htm;
location / {
proxy_pass http://test;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
include /usr/local/nginx/conf/proxy.conf;
}
}
添加 /usr/local/nginx/conf/proxy.conf
内容如下:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
保存退出!
重启nginx。
修改本地hosts文件内容:
192.168.1.1 www.test.com
测试:
访问 http://www.test.com 或 http://192.168.1.1
nginx下配置多站点其实跟apahce差不多。
打开nginx的主配置文件nginx.conf,在最后面添加:
include /etc/nginx/vhosts/*;
vhostsl里面存放着不同站点的配置,把我的例子贴上来供大家参考:
server {
listen 80;
server_name i.cteabox.com;
location / {
root /var/www/tea/blog;
index index.php index.html index.htm;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ $1$2/ permanent;
}
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/tea/blog;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/tea/blog$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
以上代码中包括了nginx的rewirte配置。
nginx下配置zend framework,可以在Linux环境下增加一段虚拟服务器的设置,设置nginx.conf如下:
listen 80;
server_name audit.local;
root /app/audit/public;
access_log /app/audit/logs/audit.access.log main;
error_log /app/audit/logs/audit.error.log;
location / {
index index.php;
# If file not found, redirect to Zend handling, we can remove the (if) here and go directly rewrite
if (!-f $request_filename){
rewrite ^/(.+)$ /index.php?$1& last;
}
}
location ~* ^.+\.(js|ico|gif|jpg|jpeg|pdf|png|css)$ {
access_log off;
expires 7d;
}
location ~ .*\.php?$ {
fastcgi_pass 127.0.0.1:36;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 http://audit.local/error;
}