wslhs 发表于 2016-12-26 09:32:32

nginx 配置相关 URL重写, 自定义404错误页面

  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 -> sites
-enabled下的站点配置文件 )


中在server 区域加入: error_page 404 = /404.php;
  或者 error_page 404 =

http://www.xxx.com/404.html
;
  =================================================



让Nginx支持shtml格式



发现包括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
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_passunix:/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;
}
页: [1]
查看完整版本: nginx 配置相关 URL重写, 自定义404错误页面