linux(nginx)

安装

必要软件准备
安装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 #关闭旧进程配合usr2升级
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版本
nginx -V #查看nginx版本,编译器版本和配置参数
curl -s http://localhost #使用curl命令来读取web信息
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;  #如果是ie就设置变量1
    }
    if ($fastcgi_script_name = ie.html){
        set $isie 0; #如果是ie.html就设置变量0
    }
    if ($isie 1){
        rewrite ^.*$ ie.html; #如果是ie就重定向到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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值