大网数据广告活动
服务器运维问答

Nginx 1.2.1 custom 404

· 52CE 我爱测

I'm trying to make a custom 404 error with nginx. First in my local machine I added the following lines to the default site: error_page 404 /custom_404.html; location = /custom_404.html { root /usr/share/nginx/html; internal; } T…

问题原文

原帖链接 · Madox · CC BY-SA 3.0

I'm trying to make a custom 404 error with nginx.

First in my local machine I added the following lines to the default site:

 error_page 404 /custom_404.html;
    location = /custom_404.html {
            root /usr/share/nginx/html;
            internal;
    }

Then in the /usr/share/nginx/html folder I created a nes file named custom_404.html.

Then I went to my development machine and tried to do the same but with aout any luck.

One of the things that I noticed is that in local I'm using the 1.9.3 version and in the development enviroment the 1.2.1 version, and also that the html forlder (/usr/share/nginx/html) doesnt exists.

And the last thing is that in development I use nginx for django aplications so my config file it's something like this:

server {
listen 80;
server_name test.example.net;

# output compression saves bandwidth
gzip  on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

client_max_body_size 10M;

if ($http_user_agent ~* '(blackberry|
    iemobile|iphone|ipod|mobile|Mobile|
    nokia|opera\s+mini|smartphone|symbian|webOS)') {
    set $mobile on;
}

if ($http_user_agent ~* 'ipad'){
    set $mobile off;
}

if ($host = example.net) {
    rewrite ^(.*)$ http://www.example.net$1 permanent;
}

if ($uri = '/') {
    set $home_es on;
}

if ($http_referer ~* ^http://test.example.net){
    set $home_es off;
}

if ($home_es = on) {
    rewrite ^(.*)$ /es$1 permanent;
}

gzip_buffers 16 8k;
gzip_disable "MSIE [1-6].(?!.*SV1)";

# media - folder in uri for admin static files
location /newmedia/ {
    expires 30d;
    root /media/jumbo_project;
}
location /media/ {
    expires 30d;
    alias /home/jumbo/media/jumbo_project/newmedia/;
}
location /static/ {
    expires 30d;
    root /media/jumbo_project;
}

error_page   502 503 504 500 /50x.html;

location /50x.html {
    internal;
}

location / {
    proxy_pass  http://localhost:8000;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout 60;
}

log_format log_time '$request_time $remote_addr - $remote_user [$time_local]  '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent"';

access_log  /var/log/nginx/jumbo-wsgi.access.log log_time;
error_log   /var/log/nginx/jumbo-wsgi.error.log;

I've run out of google results to try so if someone can give me a lead to proceed it would be great.

已采纳解答

原帖链接 · JosefScript · CC BY-SA 3.0

Well, if the folder /usr/share/nginx/html didn't exist, where did you put the error file then? Nginx serves files from whatever folder you like, you just have to tell it which one. That's why you use the root directive.

So, either create the folder /usr/share/nginx/html and put the file there or put it to e.g. /what/ever/folder and change your configuration to

error_page 404 /custom_404.html;

location = /custom_404.html { 
    root /what/ever/folder; 
    internal;
}

Simple, huh?

OK, not so simple. I don't think changing your config to a named location ("@foo") changes anything. What did you mean by "comneting and restarting". Restarting nginx or django?

Do you get any errors when reloading your nginx config?

Did you check, whether you really get a 404 when your django project is down? I have a proxy_pass in my config to proxy a webcam. When this webcam is unavailable, I get a "502 Bad Gateway".

If I catch it with error_page 502 /error_page.html; and

        location = /error_page.html  {
             root /what/ever/folder;
    }

my custom error file gets served.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。