---
title: "nginx 配置相关  URL重写， 自定义404错误页面"
author: "sinykk"
date: "2011-03-17T16:39:58Z"
tags: ["Apache", "HTML", "IIS", "PHP", "nginx"]
categories: ["Web前端"]
slug: "nginx-url-404"
draft: false
lastmod: "2012-02-22T10:16:33Z"
id: 965523
url: "https://sinykk.iteye.com/blog/965523"
---

**NGINX禁止与允许IP访问服务**

 

location / {  
            allow  122.234.54.116;  
            deny   all;  
            index  index.html index.htm index.php;  
            if (!-e $request\_filename){  
                rewrite ^(.\*)$ /index.php?s=$1 last;  
                break;  
            }  
        }

 

 

**装了NGINX1.0.8 不能访问PHP文件**

 

这个主要是PHP的解析脚本路径找不到

 

location ~ \\.php$ {  
            fastcgi\_pass   127.0.0.1:9000;  
            fastcgi\_index  index.php;  
            fastcgi\_param  SCRIPT\_FILENAME  $document\_root$fastcgi\_script\_name;  
            include        fastcgi\_params;  
        }

 

 

 

**NGINX下如何自定义404页面**

IIS和APACHE下自定义404页面的经验介绍文章已经非常多了，NGINX的目前还比较少，为了解决自家的问题特地对此作了深入的研究。研究结果表明，NGINX下配置自定义的404页面是可行的，而且很简单，只需如下几步：

**1.** 创建自己的404.php页面

**2.** 更改nginx.conf在http定义区域加入： fastcgi\_intercept\_errors on;

**3.** 更改nginx.conf（或单独网站配置文件，例如在nginx -&gt; sites -enabled下的站点配置文件 ）

中在server 区域加入： error\_page 404 = /404.php;   或者 error\_page 404 = [http://www.xxx.com/404.html](http://www.xxx.com/404.html) ;

 

=================================================

 

## [让Nginx支持shtml格式](http://www.cnblogs.com/analyzer/articles/1672316.html)

发现包括IIS在内的大多数处理服务器，都是默认不支持shtml的，nginx支持shtml的方法为：

在nginx.conf配置文件，添加：

 ssi on;  
ssi\_silent\_errors on;  
ssi\_types text/shtml;

然后保存，重启nginx即可。

 

 

在上传时nginx返回了413错误，查看log文件，显示的错误信息是:”413 Request Entity Too Large”, 于是在网上找了下“[nginx](http://www.jefflei.com/tag/nginx "标签 nginx 下的日志") 413错误”发现需要做以下设置：

 

在nginx.conf增加 client\_max\_body\_size的相关设置, 这个值默认是1m，可以增加到8m以增加提高文件大小限制；

client\_max\_body\_size 设置在

```
		location ~ .*\.(php|php5)?$
```

这个标签里，不管对配置文件做了什么改动，重启前要测试配置文件是否正确

 

如果运行的是php，那么还要检查php.ini，这个大小client\_max\_body\_size要和php.ini中的如下值的最大值一致或者稍大，这样就不会因为提交数据大小不一致出现的错误。

post\_max\_size = 8M  
upload\_max\_filesize = 2M

 

=================================================

 

**NGINX下配置 thinkphp URL重写 此配置是vhost中的**

 

```
server
	{
		listen       80;
		server_name my.ai9475.com;
		index index.html index.htm index.php;
		root /home/renshi/;
		
		error_page 404 = /404.php;



		location / {
				index index.html index.htm index.php;
				if (!-e $request_filename){
					rewrite ^(.*)$ /index.php?s=$1 last;
					break;
				}
			    }


		location ~ .*\.(php|php5)?$
			{
				fastcgi_pass  unix:/tmp/php-cgi.sock;
				fastcgi_index index.php;
				include fcgi.conf;
			}

		location /status {
			stub_status on;
			access_log   off;
		}

		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
			{
				expires      30d;
			}

		location ~ .*\.(js|css)?$
			{
				expires      12h;
			}

		error_log logs/nginx_error_renshi.log error;
	}
```