补充--关于nginx服务器多个网站如何设置404的问题?

补充--关于nginx服务器多个网站如何设置404的问题?

需求1 :设置多个网站404页面为一个

都需配置网站的nginx.conf,以上面的多网站为例,404发布目录下,每个的nginx.conf


1.知道每个网站的(nginx.conf)配置路径
www.it.com    /usr/local/nginx/conf/nginx.conf
bbs.it.com    /usr/local/nginx/conf/vhost/bbs.conf
blog.it.com   /usr/local/nginx/conf/vhost/blog.conf




2.配置每个网站nginx.conf 404路径

cd /usr/local/nginx/conf/

2.1
vim nginx.conf
添加如下内容

server {
    listen       80;
    server_name  www.it.com;

    # ... 其他配置 ...
    root  /html/www;
    index index.html index.htm;

tips:上面的不用管,关键位置在这
    # 设置404错误页面
    error_page 404 /404_blog.html;

    # 确保404页面在服务器能够访问的路径下
    location = /html/www {
        internal;
    }
}

2.2

cd /usr/local/nginx/conf/vhost/

vim bbs.conf


 server {
        listen       80;
        server_name  bbs.it.com;
        location / {
            root   /html/bbs;
            index  index.html index.htm;
   error_page 404 /404.html;

    # 确保404页面在服务器能够访问的路径下
    location = /html/www {
        internal;
        }
}
}







2.3
vim blog.conf

server {
        listen       80;
        server_name  blog.it.com;
        location / {
            root   /html/blog;
            index  index.html index.htm;
 error_page 404 /404.html;

    # 确保404页面在服务器能够访问的路径下
    location = /404.html {
        internal;
    }
        }
}



3.创建404页面
由于我们把404的位置都放在了/html/www目录下
cd /html/www
echo weihuzhong > 404.html







4.测试页面

10.0.1.106/1 这里随便输入一个不存在的页面,测试一下就可以了






 

需求2:设置每个网站不同的404页面


如果需要每个每个网站的不同404页面?

1.修改blog.conf
改一下404页面路径(这个目录必须存在,没有你要创建)

2.创建404页面
取你刚刚设置的某个404的404页面存放目录下,创建一个404页面或者你写好的,上传上去也可以

3.测试一下


posted @ 2024-03-24 13:46  三思博客  阅读(42)  评论(0编辑  收藏  举报