安装
必要软件准备
安装pcre
为了支持rewrite功能,我们需要安装pcre
yum install pcre* -y #如过你已经装了,请跳过这一步
安装openssl
需要ssl的支持,如果不需要ssl支持,请跳过这一步
yum install openssl* -y
如果没有gcc编译器需要yum install gcc -y
安装nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz
解压tar zxvf nginx-1.8.0.tar.gz
进入解压包目录执行如下命令:
./configure --prefix=/usr/local/nginx-1.8.0
--with-http_ssl_module --with-http_spdy_module
--with-http_stub_status_module --with-pcre
--with-http_stub_status_module:支持nginx状态查询
--with-http_ssl_module:支持https
--with-http_spdy_module:支持google的spdy,想了解请百度spdy,这个必须有ssl的支持
--with-pcre:为了支持rewrite重写功能,必须制定pcre
make #确定你的服务器有安装make,如果没有安装请执行yum install make -y
make && make install
cp php.ini-production /usr/local/php-5.5.29/etc/php.ini
cp /usr/local/php-5.5.29/etc/php-fpm.conf.default /usr/local/php-5.5.29/etc/php-fpm.conf
目录
conf
html
logs
sbin
nginx命令
在启动后,后台进程包含一个master进程和多个worker进程
worker进程的个数是可以设置的,一般我们会设置与机器cpu核数一致
我们要控制nginx,只需要通过kill向master进程发送信号就行了
ps aux|grep nginx #查看进程
-term,-int
-quit
-hup
-usr1
-usr2
-winch
kill -hup `cat logs/nginx.pid`
nginx在0.8版本之后,引起了一系列命令行参数:
nginx
nginx -s stop
nginx -s quit
nginx -s reload
nginx -s reopen
判断nginx配置是否正确
nginx -t -c /usr/local/nginx/conf/nginx.conf或者
nginx -t
-c [config] 为nginx指定一个配置文件,来代替缺省的
-t 不运行,而仅仅测试配置文件
出现syntax is ok和test is successful
nginx -v
nginx -V
curl -s http://localhost
80端口被占用:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
解决:
netstat -antp 查看进程
pkill -9 [程序名]
匹配
location = /index.html {
root /home/wwwroot/default/;
index index.htm index.html
}
location = / {
root /home/wwwroot/default/;
index index.html index.htm
}
location /index.htm {
root html;
index index.html index.htm
}
例子:输入127.0.0.1
先匹配=/结果是127.0.0.1/index.html
在匹配=/index.html结果是127.0.0.1/index.htm
最后匹配/index.htm结果是127.0.0.1/index.html
location ~* image #不区分大小写
location ~ image{
root /home/wwwroot/default/;
index index.html
}
例子:输入127.0.0.1/image/xxx.png
找到/home/wwwroot/default/image目录下的xxx.png
重写
location / {
if ($remote_addr = 192.168.1.100){
return 403;
}
结果ip等于192.168.1.100返回403
if ($http_user_agent ~ MSIE){
rewrite ^.*$ ie.html;
}
结果是ie就重定向到ie.html
if ($http_user_agent ~* msie){
set $isie 1;
}
if ($fastcgi_script_name = ie.html){
set $isie 0;
}
if ($isie 1){
rewrite ^.*$ ie.html;
}
if (!-e $document_root$fastcgi_script_name){
rewrite ^.*$ /404.html break;
}
输入127.0.0.1/image/xxx.png是否存在
如果不存在跳转到404.html
}
http://bbs.linuxtone.org/thread-25588-1-1.html