Custom 404 page not displaying - nginx

I just started with Nginx, and did the basic configuration, regarding configuring the PHP FPM, and I've changed the index file to be index.php instead of index.html. I also managed to get a handle on URL rewriting, although omitted from my example below for simplicity.
But in the default configuration file, there is a section dedicated to error pages, which looks as if it's ready to "plug and play". I have uncommented them, but they don't work correctly. I've gone through some 404-related questions on Stackoverflow, everyone seems to recommend that as the correct syntax.
Maybe the order of instructions is wrong, however, this is from the original sample configuration, so I don't know why it wouldn't be working.
I have remove all the commented lines from the configuration file, what's left is this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php;
server_name localhost;
location / {
try_files $uri $uri/ index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Now, when I go to http://localhost/this-page-doesnt-exist, the browser displays a literal File not found., instead of the contents of my /404.html file.
Also, the permissions of my 404.html file is the same as the permissions for the index.php, which is 644. The owner is the same as well.
What could be wrong?

try_files is passing your request for a none-existent file to index.php. This is in turn sends the request to php-fpm. By default Nginx returns the response from php-fpm to the client which is what you are seeing.
Update your php-fpm config to look like this and inform Nginx to handle error codes in the response.
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include fastcgi_params;
}
The documentation is here: fastcgi_intercept_errors

Related

I add nginx config of domain but my domain is not going online

I want to add a domain manually with cli of centos 7 and created a config file in sites-available directory:
server {
listen 80;
server_name hustana.ir www.hustana.ir;
location / {
root /home/www/public_html;
index index.html index.htm;
try_files $uri $uri/ =404;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
In my server there is another domains that are online and works fine.
if its help I should tell that at start my server was worked by Plesk Obtisian control panel
but now I wanna add another domain to server just but it`s not working.
I changed my domain NS from domain control center.
additional description:
I guess it`s good to say that if I delete the config block of one of my older domains , domain still stay online but it displays just a blank white page.
I tried add to include this config file from another directory directly into nginx.conf:
server {
listen 80;
server_name hustana.ir;
root /home/www/public_html;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
You need to enable your site as mentioned here.
Check the A-record of your domain. It must point to your server IP.

nginx sites available redirecting badly

I have a server with multiple sites running on nginx. I'm trying to add another one, and somehow I keep getting redirected to the wrong path.
What I usually do is copy from a working one and change the domain and paths, then create de symlink. I've also checked that they both have the same permissions, so I'm not seeing any mistake on my part. all paths work and of course I've restarted nginx. I also restarted the whole server out of desperation.
The failing one
server {
listen 80;
root /data/alvarezarango.com/www;
index index.php index.html index.htm;
server_name alvarezarango.com www.alvarezarango.com;
error_log /data/alvarezarango.com/logs/error.log error;
access_log /data/alvarezarango.com/logs/access.log;
location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
the working one:
server {
listen 80;
root /data/spiraxtime.com/www;
index index.php index.html index.htm;
server_name spiraxtime.com www.spiraxtime.com;
error_log /data/spiraxtime.com/logs/error.log error;
access_log /data/spiraxtime.com/logs/access.log;
location ~ [^/].php(/|$) {
fastcgi_split_path_info ^(.+?.php)(/.*)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
I get redirected to my main domain, but i can't see why. Is anything I can check to understand the redirects?

Nginx still showing the non existant URL when on the 404 page (index in my case)

I'm having a slight issue with Nginx and I'd be grateful if somebody could help me figure out the reason why it's not working.
What I'd like to do is have the website redirect to the index page when the requested URL is invalid, but when using the following code:
server {
listen 80;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name localhost;
error_page 404 /index.php;
error_page 500 502 503 504 /50x.php;
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri $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;
}
}
it's not working exactly the way I'd like it to. This code works, but the URL is still http://example.com/nonexistantpage when showing the index page. What I'd like is for the site to redirect to the index page, making the URL http://example.com
ok that's easy try this
error_page 404 =301 /;
if it doesn't work then use the full path
error_page 404 =301 http://example.com;

Nginx sites-available doesn't work

I've got this really simple server block under sites-available.
Problem: When I try to access to mydomain.com, Nginx returns a « 404 Not Found », but if I try to access to a file in particular, it works fine, like mydomain.com/index.php
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location / {
try_files $uri =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Note that:
my hosts file is configured ;
I restart Nginx after each edit ;
the access rights, user and group are correct ;
the error.log file is empty, the access.log returns me all the 404 ;
I tried to change the config by adding/removing some lines, still no changes ;
the site is enabled in sites-enabled with a correct symlink (I tried to edit it and it opened the right file) ;
I've got a few sites on the same server who runs well (so the including of sites-available and sites-enabled is OK, and Nginx works fine).
So, the answer was giver to me on ServerFault by Alexey Ten, here is a copy of the answer
Your try_files directive is too restrictive and, I guess, is in wrong place.
Either remove location / completely, it doesn't makes much sense, or, at least add $uri/ so index directive will work.
try_files $uri $uri/ =404;
But my guess is, you need to move this try_files into location ~ \.php$, this will make sure that php-file exsists before pass it to PHP-FPM for processing. All other files will be served by nginx with proper use of index directive.
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

nginx redirects every http request to https

finally i switched to the nginx webserver. But everytime i access for example http://mywebsite.com it redirects me to https://mywebsite.com. I dont have any ssl options in my server block (vhost). Here a stripped down version (only removed help comments):
server {
listen 80;
root /usr/share/nginx/www/mywebsite/htdocs;
index index.php index.html index.htm;
server_name mywebsite.com;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param CONTEXT Staging;
include fastcgi_params;
}
}
I don't really know if i am on the correct place to search for the bug?!
PS: PHP returns me ["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1"
Thanks in advice!
Got it! I've found in my /etc/nginx/fastcgi_params that line fastcgi_param HTTPS $https; which i commented out. Now, it works fine. Hope that helps someone else.

Resources