Nginx Rewrite 404 Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site nginx redirect 404 to another domain About Us Learn more about Stack Overflow the company Business Learn more about nginx 404 error hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss nginx 404 error page Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Redirect nginx config nginx error_page 404 server_name to custom 404 error page up vote 0 down vote favorite I'm new to nginx configs and have spent a lot of time googling so far. I'm trying to create a very basic nginx config file to be used in a "redirect" server. Users will be required to point naked domains (example.com) by A-record to my redirect server IP address, and the 'www' record
Nginx Custom Error Page
by CNAME to another server. The purpose of the redirect server is to then perform a 301 redirect any/wildcard naked domains back to to the 'www' version of the domain so it can be properly handled by my other server. But I also want to catch any misconfigured 'www' domains that are pointing to my server IP by A-record, and simply direct them to a custom error page on the redirect server with further instructions on how to set up their account correctly for my service. Here's what I have. It works, but since I am new to writing configs I was wondering if there is a better way to handle the redirect to the custom error page in the first server block. TIA! #redirect to error page if begins with 'www.' server { listen 80; server_name ~^www.; #only matches if starts with 'www.'. Is this good enough? rewrite ^(.*)$ /404.html; #is this the correct way to direct to a custom error page? error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } } #no match, so redirect to www.example.com server { listen 80 default_server; rewrite ^(.*)$ $scheme://www.$host$1 permanent; } redirect nginx share|i
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn
Nginx Default 404
more about Stack Overflow the company Business Learn more about hiring developers or posting nginx try_files 404 ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community nginx error_page proxy_pass Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How do I force redirect all 404's (or every page, http://stackoverflow.com/questions/23096719/redirect-nginx-config-server-name-to-custom-404-error-page whether invalid or not) to the homepage? up vote 7 down vote favorite 1 Currently every invalid page is 500 (Internal Server Error) because I probably messed up with my server block configuration. I decided to shut down my website a while ago and created a simple one-page, thank-you homepage. However old links and external sites are still trying to access other parts of the site, which no longer http://stackoverflow.com/questions/19487365/how-do-i-force-redirect-all-404s-or-every-page-whether-invalid-or-not-to-the exists. How do I force redirect all non-homepage (any invalid URL) to the homepage? I tried with the following block, but it didn't work: location / { try_files $uri $uri/ $document_uri/index.html; } My current configuration is (I don't even serve PHP files right now, ie homepage is simple html): server { server_name www.example.com example.com; access_log /srv/www/example.com/logs/access.log; error_log /srv/www/example.com/logs/error.log; root /srv/www/example.com/public_html; index index.php index.html; location / { try_files $uri $uri/ $document_uri/index.html; } # Disable favicon.ico logging location = /favicon.ico { log_not_found off; access_log off; } # Allow robots and disable logging location = /robots.txt { allow all; log_not_found off; access_log off; } # Enable permalink structures if (!-e $request_filename) { rewrite . /index.php last; } # Handle php requests location ~ \.php$ { try_files $uri = 404; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_send_timeout 900; fastcgi_read_timeout 900; fastcgi_connect_timeout 900; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # Disable static content logging and set cache time to max location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { access_log off; log_not_found off; expires max; } # Deny access to htaccess and htpasswd files location ~ /\.ht { deny all; } # Deny access to hidden files (beginning with a period) location ~ /\. { access_log off; log_not_found off; deny all; } } redirect ng
Share Vote 404 not found error may be one of the most popular error codes in the web history. But what does it exactly mean?404 not found error using Nginx, Apache, Lighttpd or any other web server, https://www.scalescale.com/tips/nginx/404-not-found-error-at-nginx/ means the file or page doesn't exist. Simply as that.What if the file exists in your web structure but you still see a 404 not found error?Then, you are facing a configuration error at your Nginx server, it is probably located at your virtual site configuration. Some of the reasons could be that you are configuring a root folder that is not correct, this is done in this way:location / { root /var/www/mysite.com; } 404 error When, for example, the real path is:location / { root /var/www/html/mysite.com; }Incorrect or missing Rewrite RulesIn a lot of cases rewrite rules are tricky and can be a pain in the ass, so, check your rules to see if you are configuring correctly, remember that apache rules won't work in Nginx, and for example, if you run a WordPress application in Nginx, and even if WordPress will generate and write the permalink structure in nginx 404 error .htaccess file, Nginx can't read .htaccess, so the result will be 404 not found error in Nginx.Additional ResourcesHTTP Status Codes on W3CPopular search terms:404 Not Found nginxnginx 404nginx errornginx 404 not found Think of us as your weekend read. Get a new article on scaling every Sunday morning and start the week out right. Get a new article on scaling every Sunday morning and start the week out right. Related PostsHow to enable Nginx server statusNginx configuration for CakePHP frameworkHow to Redirect 301 using Nginx webserverImproving Wordpress performance: Nginx + PHP-FPM + MySQL + Memcached + W3 Total CacheUpstream timed out (110: Connection timed out) while reading response header from upstream Tags 404 Not foundNginx errors Follow @scalescalehq Follow Follow Esteban Borges Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking Topics CMS Configuration (18) Framework Configuration (6) Nginx Directives (10) Nginx News (30) SysAdmin Stuff (80) Tips & Tricks (93) Tutorials & How to's (76) Recent Posts How to install LEMP on Ubuntu 14.04 How to install HHVM + Nginx on Ubuntu 14.04 LTS Nginx configuration for OsCommerce Installing Nginx on Debian 8 Creating custom error pages for Nginx on CentOS 7 Archives February 2016January 2016December 2015November 2015October 2015September 2015August 2015July 2015June 2015May 2015April 2015March 2015 AboutGreat Architectures, Stacks & DevOps at Webscale by Chris Ueland. Share Tweet Share Share Copy