Nginx 404's aren't working - They just refresh the page - redirect

My 404 page https://epicmc.us/404.php loads perfectly fine if you type it in directly, but when you get a 404 error while browsing a page like https://epicmc.us/thispagedoesntexist (doesn't exist) it just kinda refreshes the page.. Almost like I'm typing in https://epicmc.us/#
This is my config.
server {
listen 80;
listen 443;
default_type text/html;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;
}
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name epicmc.us;
error_page 404 /usr/share/nginx/html/404.php;
error_page 500 502 503 504 /50x.php;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Related

Nginx doesn't redirect "/" to "/index.php"

Nginx doesn't rewrite "/" endpoint to call index.php as default even after adding the redirect in /etc/nginx/sites-available/default.
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
#index index.html index.htm index.php;
#try_files $uri $uri/ /index.php?$args;
}
I get 403 Forbidden when I visit my-site.com, but it works correctly at my-site.com/index.php and so does mysite.com/phpmyadmin/index.php. Also, the redirect seems to work for index.html, but not index.php.
/etc/nginx/sites-available/default
server {
listen 80;
listen [::]:80;
server_name my-site.com www.my-site.com;
return 301 https://$server_name$request_url;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-my-site.com.conf;
include snippets/ssl-params.conf;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name my-site.com;
location / {
# try_files $uri $uri/ =404;
#try_files $uri $uri/ /index.php?q=$uri&$args;
try_files $uri $uri/ /index.php?q=$uri&$args;
#index index.html index.htm index.php;
#try_files $uri $uri/ /index.php?$args;
}
location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
if (!-f /var/www/html$fastcgi_script_name) {
return 404;
}
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
#fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 300;
}
location ~ /.ht {
deny all;
}
location ~ /.well-known {
allow all;
}
location /phpmyadmin {
index index.php;
}
# Rewrite rules for WordPress Multi-site.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
}
# redirect www to non-www
server {
listen 443;
listen [::]:443;
server_name www.my-site.com;
return 301 https://my-site.com$request_uri;
}
I recently had the same issue and this might be the solution:
location / {
try_files $uri $uri/index.php;
}

Nginx server removes www and 301 redirects to wrong host

I have a weird 301 redirection issue. My server is set up to handle multiple domains pointing to the server, i.e.
The main domain: https://maindomain.com
An N number of custom domains: http://somecustomdomain.com, http://anothercustomdomain.com, etc.
However there is a strange 301 issue when visiting a custom domain and including www., like: http://www.somecustomdomain.com. On the maindomain this works fine:
When visiting https://www.maindomain.com/some-uri it will redirect to: https://maindomain.com/some-uri
However, when visiting a custom domain it redirects from: http://www.somecustomdomain.com/some-uri to https://maindomain.com/some-uri (!!). You would expect it to redirect to: http://somecustomdomain.com/some-uri
I have tried debugging this issue (ensured that my browser does not cache the 301 redirects) and I have not been able to resolve the issue. I have three nginxs confiugrations inside my sites-available directory. They are listed here:
maindomain.com
catch-all (I have tried removing this file, so only maindomain.com exists, but problem still occours)
www.maindomain.com (I have tried removing this file, so only maindomain.com exists, but problem still occours)
maindomain.com contents
server {
listen 80;
server_name maindomain.com;
return 301 https://maindomain.com$request_uri;
}
server {
listen 443 ssl;
server_name maindomain.com;
root /home/forge/maindomain.com/public;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/maindomain.com/30126/server.crt;
ssl_certificate_key /etc/nginx/ssl/maindomain.com/30126/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/maindomain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
catch-all contents
server {
listen 80;
server_name ~^(.+)$;
root /home/forge/maindomain.com/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/maindomain.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
www.maindomain.com contents
server {
listen 80;
server_name www.maindomain.com;
return 301 $scheme://maindomain.com$request_uri;
}
If i CURL into http://www.somecustomdomain.com/some-uri this is the content I receive:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="1;url=https://maindomain.com/some-uri" />
<title>Redirecting to https://maindomain.com/some-uri</title>
</head>
<body>
Redirecting to https://maindomain.com/some-uri.
</body>
</html>%
You need an extra server configuration for www domains:
server {
server_name ~^(www\.)?(?<domain>.+)$;
location / {
return 301 $scheme://$domain/$uri;
}
}

Ningx non-www to www redirect causing site to show as "not available"/"server not found"

Running LEMP stack: nginx version: nginx/1.4.6 (Ubuntu)
I've tried a number of different configurations to get my non-www domain to prepend www. onto all URLs and despite double checking the following configuration against many others I continue to get errors ("not available"/"server not found"). Maybe it has something to do with the fact that I'm using a 301 redirect and not 302.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/laravel/mysite/public;
index index.php index.html index.htm;
server_name mysite.com;
return 301 $scheme://www.mysite.com$request_uri;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I define server_name then I define the rewrite directly below it. This has worked in the past but not working now. I remove the "return" line and the domain without www. works perfectly. There are no other configurations running. Can someone tell me if this configuration contains an error or if I'm attempting to do this incorrectly? Thanks.
You have to separate it into 2 server blocks like this
server {
listen 80;
server_name site.com;
return 301 $scheme://www.site.com$request_uri;
}
server {
listen 80 default_server;
server_name www.site.com;
root /var/www;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
...

Nginx redirection to https://www.domain.tld

I am trying to make my domain name only work with a https:// and www in front of it. It's important that domain.com without the www. redirects to the www, and it's also important that https:// is always enabled. I am having a lot of trouble achieving this. I've removed all the redirects from the config because they all just give me errors.
server {
listen 80;
default_type text/html;
server_name epicmc.us;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
root /usr/share/nginx/html;
index index.php index.html index.htm;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 443;
default_type text/html;
server_name www.epicmc.us;
root /usr/share/nginx/html;
index index.php index.html index.htm;
ssl on;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:5m;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP:!kEDH:!aNULL;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
ssl_certificate /etc/nginx/ssl/cert.crt;
ssl_certificate_key /etc/nginx/ssl/private.key;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
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;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
}
EDIT: I am now using a PHP redirect, but there has to be a better option...
You should define additional virtualhost, and there redirect all clients to desired method+host.
Add to your config (tune to your taste, of course) :
# redirection vhost
server {
listen 10.1.2.3:80;
server_name www.epicmc.us epicmc.us;
access_log /logs/access.log full;
error_log /logs/error.log notice;
location / {
rewrite ^/(.*)$ https://www.epicmc.us/$1 permanent;
}
}
There are two ways of doing this, simple redirect return 301
server {
server_name www.example.com;
listen 80;
return 301 https://$host$request_uri;
}
or using rewrite rules, check the answer for this question it might be helpful
server {
listen 80;
server_name www.example.com ;
location{
rewrite ^(.*)$ https://www.example.com/$1 permanent;
}
}
check answers for this question it might be helpful
Hey guys I'm using Cloudflare's flexible SSL, so my problem was that I had to do the page rules on their site and not in my config. That's why I was getting redirect errors.

Nginx 404 isn't redirecting correctly, but I have a basic idea as to why

Basically my domain just kinda redirects to the homepage if you do https://epicmc.us/nonexistantpage but https://epicmc.us/nonexistantpage.php works (My 404 error only pops up if there is a .php at the end of the non-existant page) _ Where did I go wrong? How do I make my 404 page always work?
server {
listen 80;
listen 443;
default_type text/html;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;
}
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name epicmc.us;
error_page 404 /404.php;
error_page 500 502 503 504 /50x.php;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Last argument of try_files is an URI for internal redirect. So basically any non-existent page (that not ends on php) redirects to /index.php which exists I suppose.
So I would change config to:
server {
listen 80;
listen 443 ssl;
default_type text/html;
server_name epicmc.us;
root /usr/share/nginx/html;
index index.php index.html index.htm;
error_page 404 /404.php;
error_page 500 502 503 504 /50x.php;
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I've removed location / because directives there are the same as on server level (root and index) and there is no need to have try_files.
location = /50x.html is also redundant.
And I've added ssl flag to listen 443, cause I can't imagine any reason not to have SSL on default SSL port.
EDIT: I doubt that you need fastcgi_split_info with location ~ \.php$, so I've removed it too.
maybe try this one i think you had some redundant references to the 404 pages
server {
listen 80;
listen 443;
root /usr/share/nginx/html;
index index.php index.php index.html index.htm;
server_name epicmc.us;
location ~^(?:ico|mp3|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}