How to correct my nginx configuration if it is wrong? - nginx

I am new to nginx. I try to learn using search www and stackoverflow. Mostly I get help to understand how to build my nginx configuration.
I have my domain as www.mysite.com. Everything; landing page, the error and server error must be redirect to default index.html. I also define my access and error log. All this done (below code) inside the /etc/nginx/conf.d/default.conf.
I need to redirect (proxy_pass) /admin, /user and anything related to them. Example the /admin has also different folder like /admin/login/. I need to everything after /admin must be redirected. The same goes also for the /user as well.
1) Looking at my code am I redirect the location /admin and location /user correctly?
2) I also use try_files $uri $uri/ =404; in redirection. which also redirects the 404 to default index.html. Am I doing right?
3) I am also denying access to some file and folder. Am I doing right?
My main question is How to correct my nginx configuration if it is wrong? So to understand the correct nginx configuration I divide my question to 3 different question above. I hope I didnt brake stackoverflow how to ask question guidelines.
Thank you.
UPDATE:
server {
charset UTF-8;
listen 80;
listen [::]:80;
server_name www.mysite.com;
access_log /var/log/nginx/host.access.log main;
error_log /var/log/nginx/host.error.log main;
# define a root location variable and assign a value
set $rootLocation /usr/share/nginx/html;
# define www.mysite.com landing page to the static index.html page
location / {
root rootLocation;
index index.html index.htm;
}
# define error page to the static index.html page
error_page 404 /index.html;
location = /index.html {
root rootLocation;
internal;
}
# redirect server error pages to the static index.html page
error_page 500 502 503 504 /index.html;
location = /index.html {
root rootLocation;
internal;
}
# redirect www.mysite.com/admin to localhost
# /admin or /admin/ or /admin/**** must be redirect to localhost
location /admin {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3000";
}
# redirect www.mysite.com/user to localhost
# /user or /user/ or /user/**** must be redirect to localhost
location /user {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3001";
}
}

It is usual to place the root statement once in the server block, rather than repeat the same value in multiple location blocks.
You are using proxy_pass to change the URI before passing it upstream. In this case, the location value and the URI part of the proxy_pass value should either both end with / or neither end with /. See this document for details.
Usually you do not want to place try_files and proxy_pass in the same location. This causes Nginx to check for the existence of the file in its document root before allowing the request to pass upstream.
You should not need to deny access to the configuration files, as these file should not be within the document root in the first place.

Related

Site is not redirecting to backend node NGINX config

I have a react frontend with a node backend but my nginx setup is currently not working. What I want to do is redirect https://sales.example.com/api/stats/get_customer_count/2323232 to http://127.0.0.1:3000/stats/get_customer_count/2323232(Node backend server runs on http://127.0.0.1:3000) but I keep getting 404 errors stating that the api path is not found. Not sure how to go about fixing this. Appreciate it. Thanks
server {
root "/home/sales/frontend/dist";
index index.html index.htm;
server_name sales.example.com;
error_log /var/log/nginx/error.log;
listen 80;
location / {
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location ~* ^/api/ {
rewrite ^/api/(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}
This seems to be answered here already, and the explanation written by #Dayo is good.
Translated it to your example, it looks like this: (notice, the main difference is the tailing slash in the proxy pass)
location ~* ^/api/ {
proxy_pass http://127.0.0.1:3000/;
}
But please, read through the linked answer, before copying this over.

Nginx says 404 Not Found when trying to serve an image from persistent storage

Following is my nginx config -
server {
server_name example.com www.example.com;
root /volume-bde-01/images;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://<server-ip>:3000;
}
location /images {
}
}
When I call http://example.com/api, it works fine and returns me the response from my server running at port 3000. I need to upload images as a part of an api and need to directly display them later using the image src url (I was thinking). But some how the configuration doesn't work and gives mein 404 - Not Found.
/volume-bde-01/images; is a persistent storage block attached to the server.
I tried http://example.com/images/test.jpg
NOTE - I believe its not mandatory to put the images in the same directory as running server's root as most of the examples are around that? We can place them anywhere, Isn't it?
P.S. -
It works for me with -
location /images/ {
alias /volume-blr1-01/images/;
}
But I wonder what difference is between root and alias ?
The alias directive tells Nginx to replace what is defined in the location block with the path specified by the alias directive.
location ^~ /images {
alias /var/www/static;
try_files $uri $uri/ =404;
}
http://example.com/images/logo.png into the file path /var/www/static/logo.png
http://example.com/images/third-party/facebook-logo.png into the file path /var/www/static/third-party/facebook-logo.png
The root directive tells Nginx to take the request url and append it behind the specified directory.
location ^~ /images {
root /var/www/static;
try_files $uri $uri/ =404;
}
http://example.com/images/logo.png into the file path /var/www/static/images/logo.png.
http://example.com/contact.html into the file path /var/www/example.com/contact.html
Credits: https://www.techcoil.com/blog/understanding-the-difference-between-the-root-and-alias-directives-in-nginx/

Dokku redirects to another domain when requested site is down

I have Dokku installed on a server, with multiple sites/domains deployed to it. When one of my sites goes down, all HTTP requests to it get redirected (for some reason) to another site. This is confusing. I'm expecting Dokku to show some error page in this case. Is it the default behavior or I did something wrong?
PS. This is the problem: https://github.com/dokku/dokku/issues/2602
How about adding a custom error page based on the error code by editing vhost file:
server{
server_name www.foo.com;
root /srv/www/foo/public_html;
expires 1M;
access_log /srv/www/foo/logs/access.log;
error_log /srv/www/foo/logs/error.log;
error_page 404 /404.html;
location / {
index index.html;
rewrite ^/(.*)/$ /$1 permanent;
try_files "${uri}.html" $uri $uri/ =404;
}
location = /404.html {
internal;
}
}
Your server error might be caught from codes 404 or 500

nginx Redirect subfolder to root domain

I want to redirect the subfolder and all contents to root domain.
For example:
http://www.example.com/ubb/ will redirect to http://www.example.com
My server configuration is like below:
server {
listen 80 default_server;
root /home/vishant/devcenter/wava-v1.1/HTML;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name baetter.l;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
#proxy_pass "http://127.0.0.1:3000";
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
i have found similar problem solved using htaccess here
But how can i achieve in nginx??
One of a number of solutions is:
location ^~ /ubb/ {
return 302 /;
}
The ^~ modifier ensures that this prefix location continues to take precedence if you were to add any regex locations in the future. See this document for details.
The return directive is documented here.

Nginx custom configuration

I need my NGINX configuration to do some unique things based on the URL.
If the URL is:
mydomain.com/blog OR mydomain.com/blog/
I need NGINX to serve up a ghost blog on port 2368 I have done this by:
location /blog/ {
proxy_pass http://localhost:2368;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
}
If the URL is:
mydomain.com/some-article-title
I need NGINX to direct to some-article-title.html. I have done this by:
location / {
try_files $uri $uri/ $uri.html =404;
}
FIRST QUESTION: I would also like this to work if the url is mydomain.com/some-article-title/ OR if the case is different such as mydomain.com/Some-article-Title. How can I do this in NGINX?
SECOND QUESTION: I also need NGINX to redirect to a specific subdomain if there is no matching HTML file i.e. If the URL is mydomain.com/jimsmith and there is no HTML file jimsmith.html I need NGINX to take the user to jimsmith.mydomain.com How can I do that?
location /blog/ won't match requests for /blog, but location /blog will.
try_files... try files. Thus, if your request contains a trailing slash, nginx will search for a directory (files ending with / are directories only on any filesystem).
If you wish nginx to search for a file when a requests contains a trailing slash, you will need to rewrite the request to its pre-trailing-slash form. Note that directories won't be accessible anymore... so the most generic form showed below is far from being recommended. The trailing slash kind of rewrite is discouraged at all in any form anyway...
location ~* ^(?<article>/[^/]+)/$ {
return 302 $scheme://$host$article;
}
You cannot have several files having the same name with letters capitalized or not in the filesystem, such as some-article-title and Some-article-Title, so a search for either name will always succeed with the existing file.
If you want to redirect files not matched by try_files first parameters, the last on can be used as a fallback to a named location, where you can issue a redirect (see try_files documentation).
location / {
try_files $uri $uri/ $uri.html #notexisting;
}
location #notexisting {
rewrite ^/(?<subdomain>.*)$ $scheme://$subdomain.mydomain.com;
}

Resources