I have a static website with Next-Js export, and my Nginx setting is like -
server {
server_name www.example.com;
root /var/www/example;
index index.html index.htm;
location / {
try_files $uri $uri.html /$uri /index.html;
}
listen 80;
}
This works well but I can not get a 404 error page if we manually update the wrong URL on the client side.
Can anyone explain how to get an error 404 page for not matching URL.
For now, if a manually update URL on the browser, the same page opens which was opened from before.
Thanks
Maybe this help someone -
After reading about uri and how Nginx manages order of uri, here is what I came up with :
Ps - I'm still experimenting with this code for now)
server {
server_name www.example.com;
root /var/www/example;
index index.html index.htm;
location / {
try_files $uri.html $uri/ /$uri /404.html;
# $uri # /index.html;
}
listen 80;
}
Related
I have an IP address of my server that I want to put my website Frontend and Backend admin. The site1 part is simply should be at "http://IP/" and and site2 should be in "http://IP/admin" .
I have installed Nginx in server and my websites files are inside: Lets say its like :
site1: /var/www/html/site1/index.html
site2: /var/www/html/site2/index.html
I created 2 files in /etc/nginx/site-available/ called "site1.conf" and "site2.conf" .
site1.conf:
server {
listen 80;
listen [::]:80;
root /var/www/html/site1;
index index.html index.htm;
server_name http://myIP;
location / {
try_files $uri $uri/ =404;
}
}
site2.conf:
server {
listen 80;
listen [::]:80;
server_name http://myIP;
location /admin {
autoindex on;
alias /var/www/html/site2;
try_files $uri $uri/ /index.html last;
index index.html;
}
}
Then I linked these 2 files into "/etc/nginx/site-enabled"
After restarting the Nginx, my "http://ip/" opens site1 "index.html" and works fine.
but "http://ip/admin/" gives 404 error instead of opening site2 "index.html"
http://IP/ and http://IP/admin both point to the same server, with the server_name "IP".
Your server contains at least two location blocks.
For example:
server {
listen 80;
listen [::]:80;
server_name 1.2.3.4;
root /var/www/html/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location /admin {
alias /var/www/html/site2;
...
}
}
The server name only contains the text of the IP address or the DNS name. See this document for more.
You can spread your configuration across as many files as you choose. See the include directive.
The nginx configuration is a file called nginx.conf and contains an include statement to source all of the files in the sites-available directory. The content of these files are contained within the http { ... }.
As I have already stated, your two services are one server { ... } block, as far as nginx is concerned. However, you can still create a server block file in sites-available that includes files from some other location. Just don't use sites-avalable or conf.d, as nginx is aready using those directory names.
For example:
In sites-available/mysites.conf:
server {
listen 80;
listen [::]:80;
server_name 1.2.3.4;
include /path/to/my/location/confs/*.conf;
}
And in /path/to/my/location/confs/site1.conf:
root /var/www/html/site1;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
And in /path/to/my/location/confs/site2.conf:
location /admin {
alias /var/www/html/site2;
...
}
I am not saying that this is a good way to organise your files, but with nginx, many things are possible.
so my vps is running ubuntu 17.04 and i have configured my dns pointing to my vps on both domain #
here is my nginx folder structure
this is my server block code for w3saver.com
filepath: /etc/nginx/sites-available/w3saver.com
`server {
listen 80;
listen [::]:80;
server_name w3saver.com www.w3saver.com;
root /var/www/w3saver.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
} }`
and this server block is for w3youtube.com
filepath: /etc/nginx/sites-available/w3youtube.com
`server {
listen 80;
listen [::]:80;
server_name w3youtube.com;
root /var/www/w3youtube.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
} }`
so my problem is when ever i go to "w3youtube.com" page i get redirect to "w3saver.com" page (both domains are serving same page) so what is wrong with that i can't able to figured it out!
ps: i am expecting both domain should serve dedicated static html pages. insted of single html on both domains
this is what i am getting right now.
result in domain1
also same result in domain2
thanks in advance.
I'm starting to go nuts at this. For some reason, routing wont work on my single page application. So www.example.com works, but not www.example.com/service. I read a lot of posts on how to fix it, but nothing seems to work.
This is my config file at /etc/nginx/conf.d/App.conf
server {
listen 80;
server_name example.com *.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/to/certificate/;
ssl_certificate_key /path/to/certificate/key;
root /var/www/App/public;
index index.html;
location / {
try_files $uri /index.html;
}
ssl_session_timeout 5;
}
I have tried all kind of different "location" routes, and nothing seems to work. I do also restart the service with "sudo service nginx restart" everytime I change.
Any clues?
In the comments you said there's a small fixed set of possible routes. In that case you can add a location block for each route, with an alias to the top-level, for example,
location / {
try_files $uri $uri/ =404;
}
location /services {
alias /var/www/App/public;
try_files $uri $uri/ =404;
}
Edit: Or, if you want to serve the top-level index.html in response to any request at all,
location / {
try_files /index.html =404;
}
What I'm trying to achieve is if someone visit my home/index page, I need to server my index.html file. But, if it's another URL/path pass the request to my index.php file (I'm using Symfony).
I follow this example, but is not working. It's always serving my PHP file.
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /index.html
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}
I will appreciate any help or guidance you can share with me.
This finally worked for me:
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}
I have two servers that have the same root but different indexes. The second server is a subdomain of the first server, so I want to serve all the same files except the only thing I want different is the index.
Everything works fine except for the second location part on the second server giving me 403 forbidden error on every page I visit, yet it works just fine on the first server.
If I go to domain.com/foo/ then I get the 403 forbidden. If I go to domain.com/foo/index.html, then I don't get the 403 forbidden and get served the correct files.
Any help would be appreciated.
Here is my nginx.conf:
server {
listen 80;
listen 443 ssl;
root C:\Users\Lansana\Documents\Github\foo;
index index.html;
server_name domain.org;
location ~ /.+ {
try_files $uri $uri/ /app.html =404;
}
location / {
try_files $uri $uri/ /index.html =404;
}
}
server {
listen 80;
listen 443 ssl;
root C:\Users\Lansana\Documents\Github\foo;
index subdomain.html;
server_name subdomain.domain.org;
location ~ /.+ {
try_files $uri $uri/ /app.html =404;
}
location / {
try_files $uri $uri/ /index.html =404;
}
}
The solution to this was to add a second file for the index on my second server.
Replace:
index subdomain.html
with:
index subdomain.html index.html