nginx 虚拟主机 404页面定义 的几种方式

喜你入骨 提交于 2020-02-26 02:27:32

有两个网站一个lnmp环境,一个web工程用nginx反向代理

***域名做隐藏处理

网站1.

虚拟主机路径所在目录

[root@kangcw nginx]# pwd
/www/server/panel/vhost/nginx
[root@kangcw nginx]# ls
www.xxx.com.conf  www.xxx2.com.conf

虚拟主机一lnmp环境 

Nginx访问一个静态的html 页面,当这个页面没有的时候,Nginx抛出404,那么如何返回给客户端404呢? 这种情况下不需要修改任何参数,就能实现这个功能。

如果后端是php解析的,需要加一个变量

在http段中加一个变量 fastcgi_intercept_errors on 就可以了。

[root@kangcw nginx]# cat www.xxx1.com.conf |grep -v "#" 
server
{
	listen 80;
	server_name www.xxx1.com;
	index index.php index.html index.htm default.php default.htm default.html;
	root /www/wwwroot/wbn4.cn/;
	location / {
		index  index.html index.htm index.php;
         	if (!-e $request_filename) {
              	rewrite ^/(.*)$ /index.php?s=$1 last;
              	break;
         		}
         autoindex  on;
    		}
    	fastcgi_intercept_errors on;
    	location ~* ^/(uploads|Public|static|template)/.*.(php)$ {
    		deny all;
    	}
    error_page 404 /404.html;
    error_page 502 /502.html;
    include enable-php-56.conf;
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    	{
        	expires      30d;
        	error_log off;
        	access_log /dev/null;
    	}
    	location ~ .*\.(js|css)?$
    	{
        	expires      12h;
        	error_log off;
        	access_log /dev/null; 
    	}
    	access_log  /www/wwwlogs/wbn4.cn.access.log;
    	error_log  /www/wwwlogs/wbn4.cn.error.log;
}

虚拟主机二

tomcta

如果后台Tomcat处理报错抛出404,想把这个状态叫Nginx反馈给客户端或者重定向到某个连接,配置如下:

[root@kangcw nginx]# cat www.xxx.com.conf 
server {
    listen       80;
    #listen       [::]:80 default_server;
    server_name  www.xxx.com;
	proxy_intercept_errors on;     ### 关键参数:这个变量开启后,我们才能自定义错误页面,当后端返回404,nginx拦截错误定义错误页面
        #proxy_buffer_size 64k;
        #proxy_buffers   32 64k;
        #proxy_busy_buffers_size 64k;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


    # Load configuration files for the default server block.
    #include /etc/nginx/default.d/*.conf;

    location / {
          proxy_buffer_size 64k;
          proxy_buffers   32 64k;
          proxy_busy_buffers_size 64k;
          #root   /www/server/panel/vhost/html;   
          index  index.html index.htm;
          add_header 'Access-Control-Allow-Origin' '*';
          add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
          add_header 'Access-Control-Allow-Headers' 'userId,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          proxy_pass http://127.0.0.1:8080 ;
    }

error_page    404  /404.html;

location = /404.html {

    root         /www/server/panel/vhost/html;  #网站跟目录

}
        access_log  /www/wwwlogs/dzgk.access.log;
        error_log  /www/wwwlogs/dzgk.error.log;
}

测试2个虚拟主机的404

网站一的

网站二的

指定一个错误页面

error_page    404  /404.html;

location = /404.html {

root   /usr/share/nginx/html;

}

指定一个错误URL

error_page 404  /404.html;

error_page 404 = http://www.test.com/error.html;

网站三 

nginx version: nginx/1.16.1

使用默认的404配置死活不行 修改如下  记录一下

[root@iZ2vc7sf7l7su9un17u90aZ html]# cat /etc/nginx/nginx.conf|grep -v "^#"

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        server_name  aaaa.bbb.com;
        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
	#fastcgi_intercept_errors on;
	  proxy_intercept_errors on;
	  proxy_buffer_size 64k;
          proxy_buffers   32 64k;
          proxy_busy_buffers_size 64k;
          index  index.html index.htm;
          add_header 'Access-Control-Allow-Origin' '*';
          add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
          add_header 'Access-Control-Allow-Headers' 'userId,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
          proxy_pass http://127.0.0.1:8080 ;

        }

        #error_page 404 /404.html;
        #    location = /40x.html {
        #}

    error_page 404 500 502 503 504  /404.html;
    location = /404.html {
       root /usr/share/nginx/html;
    }


    }


}

 

 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!