-1

I want Nginx to redirect all pages that are 404 to my index.html.

This nginx is running as a container. Yes I have confirmed nginx is actually being reached as the docker-compose log output shows the GET requests and the redirect to the 404 page. Have also confirmed the same config file is being used and without any errors. Here is the current version output

nginx version: nginx/1.25.1
built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4)
built with OpenSSL 3.0.7 1 Nov 2022 (running with OpenSSL 3.0.9 30 May 2023)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer -g' --with-ld-opt=-Wl,--as-needed,-O1,--sort-common

here is my nginx.conf currently. I have read everything and tried everything on this page, and this page none of it works. No matter what nginx will still land on the page in the URL, and give the nginx 404 page instead.

Why?

# ADJUSTED nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;
    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;

        location / {
            rewrite (.*) /usr/share/nginx/html/index.html last;
        }

    }

}

1 Answer 1

1

Woops, I figured it out. This page answers my question.

Problem was this line

include /etc/nginx/conf.d/*.conf;

Commented it out, and my actual config was then used!

I also adjusted the conf file to look like this exactly.

Main change was this

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
# ADJUSTED nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;
    keepalive_timeout  65;

    #include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        listen  [::]:80;
        server_name  localhost;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

    }
}
5
  • 1
    +1 use of the "try_files ... /index.html" directive rather than forcing 404 Nginx error handler... this is a clean implementation and follows Best Practices.
    – bellasys
    Oct 15 at 19:14
  • I think you can also accept your own answer...
    – bellasys
    Oct 15 at 19:15
  • Have to wait 2 days to accept my answer :(
    – Dave
    Oct 15 at 20:08
  • 1
    A good practice is to place your configs in a .conf file in conf.d directory. With current setup, your configuration can be overwritten whenever nginx package is updaterd. Oct 15 at 21:01
  • I would suggest that you take a look at the.conf.d directory to see what's inside there
    – djdomi
    Oct 16 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .