linux(nginx)

来源:互联网 发布:日本文化史知乎 编辑:程序博客网 时间:2024/06/08 17:33

安装

必要软件准备安装pcre为了支持rewrite功能,我们需要安装pcre yum install pcre* -y #如过你已经装了,请跳过这一步安装openssl需要ssl的支持,如果不需要ssl支持,请跳过这一步 yum install openssl* -y如果没有gcc编译器需要yum install gcc -y安装nginxwget 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重写功能,必须制定pcremake #确定你的服务器有安装make,如果没有安装请执行yum install make -ymake && make installcp php.ini-production /usr/local/php-5.5.29/etc/php.inicp /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.htmllocation ~* 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

0 0