nginx : having issue with setting up multi domain on single ip(vps) - nginx

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.

Related

URI setup in Nginx for Next Js export

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;
}

nginx: how to use multiple roots for multiple locations, including / ? (what's wrong with my paths config?)

I'm trying to setup nginx for first test uses, without a domain yet.
My current goal is to show some page at http://<server IP> and serve some static content at http://<server IP>/projectname. The "some page" is currently just the default /var/www/html/index.nginx-debian.html.
In /etc/nginx/sites-available/ I've created a projectname config and I've put a link to sites-enabled:
sudo ln -s /etc/nginx/sites-available/tiddlywiki /etc/nginx/sites-enabled/
The first version of config was
server {
listen 80;
listen [::]:80;
server_name <server IP>;
root /some/path/to/project/static-files;
index index.html;
location /projectname {
try_files $uri $uri/ =404;
}
}
What I got, is http://<server IP> started serving static files, but http://<server IP>/projectname showed 404. How do I fix that? Because next step, I've followed this answer and tried to set 2 locations:
location /projectname {
try_files $uri $uri/ =404;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
but only got the default page at http://<server IP> back again, and 404 at http://<server IP>/projectname.
Ok, so the problem was, with root directive, path is concatenated to the root, so with this config
server {
listen 80;
listen [::]:80;
server_name <server IP>;
root /some/path/to/project/static-files;
index index.html;
location /projectname {
try_files $uri $uri/ =404;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
}
nginx tried to serve /projectname → /some/path/to/project/static-files/projectname which is an unexisting folder (existing one is /some/path/to/project/static-files). What I needed is the alias directive:
server {
listen 80;
listen [::]:80;
server_name <server IP>;
index index.html;
location /projectname {
alias /some/path/to/project/static-files;
index index.html;
}
location / {
root /var/www/html;
index index.nginx-debian.html;
}
}
I'm not sure how exactly try_files works so I've removed it for now and also added the index directive.

nginx config does not take action

I'm trying to setup a basic nginx server. Usually I'm not working with nginx and I ran in to some issues I'm unable to wrap my head around.
I have a debain server with all the necessary things installed (like php, mariadb, ufw,...) and I want to run my website somewhat like this:
xxx.xxx.xxx.xxx -> /var/www/
http(s)://(www).lechner.io -> /var/www/domains/lechnerio/
I want both https and http and the domain with www and without pointing to the folder /var/www/domains/lechnerio and the IP Address pointing to /var/www/
First things first, only getting :80 working.
I have the following config setup:
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
however, when I now try to visit the IP the nginx welcome site is shown. When I access it via domain, it also shows the files from /var/www/ even though i reloaded everything. nginx -t is working. A link from /etc/nginx/sites-available/lechnerio to /etc/nginx/sites-enabled/
Any input very welcome!
try following
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io www.lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.html index.htm;
server_name _; # everything else
location / {
try_files $uri $uri/ =404;
}
}

nginx don't reconoize subdomain, it redirect to default page always

I have a problem with setting nginx:
I have a subdomain on goDaddy: allies.ejample.com.co
On digitalOcean I have nginx with this server block
server {
listen 80;
listen [::]:80;
root /var/www/allies;
index index.html index.htm index.nginx-debian.html;
server_name allies.example.com.co www.allies.example.com.co;
location / {
try_files $uri $uri/ =404;
}
}
when I type in browser allies.example.com.co it redirect to default page into /var/www/html into this folder is default page of nginx.
What do I am wrong ?? ...
Somebody Can help me?

Nginx reverse-proxy and root problem in multiple websites

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.

Resources