码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx配置文件说明

时间:2014-08-22 19:50:49      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:discuz   style   http   os   使用   io   文件   for   ar   

 -- 重定向Nginx错误页面的方法 --

 

在nginx的配置文件中,可以对发生错误是的重定向页面进行配置,在nginx.conf文件中有下面的配置信息:

error_page 404  /404.html;

这个404.html保证在nginx主目录下的html目录中即可,如果需要在出现404错误后直接跳转到另外一个地址,可以直接设置如下:


error_page 404 http://www.***.com;


同样的方式可以定义常见的403、500等错误。


特别注意的是404.html文件页面大小要超过512k,不然会被ie浏览器替换为ie默认的错误页面。

 

                         -- Nginx状态监控配置 --

 

需要注意的是,Nginx默认安装不包含状态模块stub_status,所以,在编译Nginx的时候,需要添加以下参数:

--with-http_stub_status_module

一旦包含stub_status模块后,我们就可以在配置文件nginx.conf中开启状态页面:

http { server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /nginx-status { stub_status on; access_log off; } } }

以上配置文件中,我们实际增加的部分是:

location /nginx-status { stub_status on; access_log off; }

同样,假如Nginx所在服务器的IP为10.0.0.1,同时指向它的域名为www.domain.com,这样一来,Nginx的状态页面便是:

http://10.0.0.1/nginx-status

或者

http://www.domain.com/nginx-status

同样,建议您将以上示例中的nginx-status修改为其它字符串。

另外,Nginx的stub_status也支持授权IP的配置,您可以参考Nginx的手册,监控宝提供的服务监控点IP地址为:

60.195.252.106 60.195.249.83


                               -- Apache监控配置 --

 

修改httpd.conf

在添加Apache监控之前,我们需要开启Apache的status模块,以Apache2.x版本为例,我们需要修改httpd.conf,增加以下配置段:

ExtendedStatus On <Location /server-status> SetHandler server-status Order deny,allow Deny from all Allow from 60.195.252.106 Allow from 60.195.249.83 </Location>

这样一来,假如你的Apache所在服务器的域名和IP地址为

www.domain.com -> 10.0.0.1

那么,Apache的状态页面地址便为:

http://www.domain.com/server-status

或者

http://10.0.0.1/server-status

受限访问设置

我们当然不希望其它人浏览status页面,所以您可以做一些限制,首先,对于默认的status地址,您可以进行修改,比如将:

<Location /server-status>

修改为:

<Location /my-domain-status>

其次,您已经看到,我们提供了指定的授权IP地址:60.195.252.106、60.195.249.83,您可以仅授权这个地址访问您的status页面。

 

                                 -- Nginx静态文件处理 --

 

通过正则表达式,我们可让 nginx 识别出各种静态文件,例如 images 路径下的所有请求可以写为:

location ~ ^/images/ {

    root /opt/webapp/images;

}

而下面的配置则定义了几种文件类型的请求处理方式。

location ~ .(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {

    root /opt/webapp;

    expires 24h;

}

对于例如图片、静态 HTML 文件、js 脚本文件和 css 样式文件等,我们希望 Nginx 直接处理并返回给浏览器,这样可以大大的加快网页浏览时的速度。因此对于这类文件我们需要通过 root 指令来指定文件的存放路径,同时因为这类文件并不常修改,通过 expires 指令来控制其在浏览器的缓存,以减少不必要的请求。 expires 指令可以控制 HTTP 应答中的“ Expires ”和“ Cache-Control ”的头标(起到控制页面缓存的作用)。您可以使用例如以下的格式来书写 Expires:

expires 1 January, 1970, 00:00:01 GMT;

expires 60s;

expires 30m;

expires 24h;

expires 1d;

expires max;

expires off;

这样当你输入http://127.0.0.1/1.html的时候会自动跳转到:

 /opt/webapp/1.html

expires 1 January, 1970, 00:00:01 GMT; expires 60s; expires 30m; expires 24h; expires 1d; expires max; expires off;

location ~ .(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ { root /opt/webapp; expires 24h; }

                        -- Nginx设置上传目录无执行权限 --

在windows+iis下,可以设置上传目录,类似:upload,uploadfile,attachments,这样的目录下面无脚本执行权限,从而防止非法用户上传脚本得到webshell

nginx上也很简单,我们使用location。。如下:

location ~ ^/upload/.*.(php|php5)$

{

deny all;

}

其中upload换为你要设置的目录名字

这条规则的含义是匹配请求连接中开头是/upload/,中间匹配任意字符,结尾匹配.php或者.php5的页面,最后利用deny all禁止访问,这样就防止了上传目录的脚本执行权限

 

禁止访问http://localhost/path/

location = /path/ {

return 404;

}

 

禁止访问http://localhost/test/test.php

location ^~ /test

{

deny all;

}

 

禁止访问某个(test)目录

location ~^/(test)/{

 deny all;

}

 

                          --Nginx Rewrite设置 --

首先,Nginx可以用if进行条件匹配,语法规则类似C,举例如下:


if ($http_user_agent ~ MSIE) {

rewrite  ^(.*)$  /msie/$1  break;

}


1、正则表达式匹配,其中:


* ~  为区分大小写匹配

* ~* 为不区分大小写匹配

* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配


2、文件及目录匹配,其中:


* -f和!-f用来判断是否存在文件

* -d和!-d用来判断是否存在目录

* -e和!-e用来判断是否存在文件或目录

* -x和!-x用来判断文件是否可执行


如:


if (!-f $request_filename) {

proxy_pass  http://127.0.0.1;

}


其次,Nginx的Rewrite规则与Apache几乎完全一致,所不同的是最后的flag标记,举例如下:


rewrite ^/feed/$ http://feed.shunz.net last;


flag标记有:


* last 相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则

* break 与last类似

* redirect 返回302临时重定向

* permanent 返回301永久重定向


Wordpress的重定向规则:


if (!-e $request_filename) {

rewrite ^/(index|atom|rsd).xml$ http://feed.shunz.net last;

rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;

rewrite ^([_0-9a-zA-Z-]+)?(/.*.php)$ $2 last;

rewrite ^ /index.php last;

}


Discuz!的重定向规则:


if (!-f $request_filename) {

rewrite ^/archiver/((fid|tid)-[w-]+.html)$   /archiver/index.php?$1 last;

rewrite ^/forum-([0-9]+)-([0-9]+).html$   /forumdisplay.php?fid=$1&page=$2 last;

rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+).html$  /viewthread.php?tid=$1&extra=page=$3&page=$2 last;

rewrite ^/space-(username|uid)-(.+).html$   /space.php?$1=$2 last;

rewrite ^/tag-(.+).html$ /tag.php?name=$1 last;

}

 

                                     -- Nginx防盗链处理 --

location /photos/ {

valid_referers none blocked www.mydomain.com mydomain.com;

if ($invalid_referer) {

return 403;

}

}


Nginx配置文件说明

标签:discuz   style   http   os   使用   io   文件   for   ar   

原文地址:http://my.oschina.net/u/231017/blog/305687

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!