Nginx Custom error page for Individual directory - nginx

This my nginx configuration for 404 custom error page. But my problem is I'm getting custom 404 error page for all my 404 error that occurs in my server. I want to get custom error page for a particular directory only.
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
root /var/www/html;
try_files $uri $uri/ =404;
}
error_page 404 /fail.html;
location = /fail.html {
root /var/www/html/checking/errors;
internal;
}
location /testing/([a-zA-Z]+)/ {
root /var/www/html/testing/$1;
try_files $uri $uri.html;
}
}

Assuming that what you're asking for is that when people attempt to view files of the form /testing/does-not-exist then they will see you error file, you want to move the error_page 404 /fail.html to within that location block. Where you have it right now (within the server block), it will apply to all requests. If it's for a different path that you'd like the error to show up, create a new location block for that path and then add the error_page directive to that block.

Related

nginx to always serve the root index.html in every path

I have currently the code below. I am wondering if it's possible to still service this root even though I go to other pages like http://localhost/dog. The problem with my command below is it will return 404
server {
listen 80;
server_name localhost;
location / {
root /usr/src/app/angularjs/dist;
}
}
It is possible. Add the try_files directive to your location block, this will tell nginx to load all requests that cannot be matched to a filesystem path with your index.html:
try_files $uri /index.html;

Serving static content with nginx alias, what is the difference between /dir and /dir/?

I'm setting up an nginx server to serve an index.html page that I have stored in /data/www/test/.
Here is my config file (/etc/nginx/sites-enabled/default):
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location /test/ {
alias /data/www/test/;
index index.html;
}
}
I can curl 0:80 and see the contents of /usr/share/nginx/html/index.html.
I can curl 0:80/test/ and see the contents of /data/www/test/index.html.
But when I curl 0:80/test I get a 404.
I want to be able to see that html whether I type /test or /test/.
Can someone please explain what is happening here?
Thanks!
Just like the format, request for /test return test file, the config will match location / and find test file in root path. you can try doing touch /usr/share/nginx/html/test and access /test again
Request for /test/ will match location /test/, at the content phase, it look the index file in the root/alias specified location.

Nginx configuration for displaying contents in home directory

The requirement is to display contents in local user's home directory when you enter /~user.
www.blahblahblah.com/~user
I've created a local user with home directory and configured Nginx like that:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm test.html;
# Make site accessible from http://localhost/
server_name localhost;
location ~* /\~(.*)/$ {
root /home/$1/;
}
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
}
}
But I still got 404 not found error.
The problem is that the root and the URI are concatenated to obtain the request filename, so you are asking nginx to deliver root /home/user/~user/.
The solution is to use a rewrite ... break to sanitise the URI. You can extend the regular expression to capture both elements and then use the named captures in a root and a rewrite ... break statement:
location ~* /\~(?<username>\w+)(?<userpath>/.*)$ {
root /home/$username;
rewrite ^ $userpath break;
}

NGINX alias with HHVM

I'm trying to reroute one of my web directories to another project directory in the home folder. But when I try and access the page, I get served a blank file
I believe the fault in on the try_files $uri $uri/ /rtstaging/index.php?$query_string; line. But I'm new to using nginx, so any help would be much appreciated
server {
listen 80;
listen [::]:80;
root /home/default/public;
index index.php index.html index.htm;
server_name nu-voo.co.uk www.nu-voo.co.uk;
include hhvm.conf;
location /rtstaging/ {
alias /home/rtsearch/public/;
try_files $uri $uri/ /rtstaging/index.php?$query_string;
include hhvm.conf;
}
}

Getting 405 when posting to php file

I have an html form which posts to to mysite.com/s/form.php, but when I submit the form I the error 405 Not Allowed. Could this be a permissions issue? Or is it something else? I'm stuck at this point.
This is my current config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name localhost;
root /var/www/mysite.com/site/;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
location /s/ {
alias /var/www/mysite.com/s/;
}
}
Could someone please point out what's going wrong here?
I forgot to include the bit that makes php work, so nginx thought it was the POST was being sent to a static file, which nginx does not allow.

Resources