Nginx reverse-proxy and root problem in multiple websites - nginx

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.

Related

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 with multiple locations directives with subdomains

I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;

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

Accept all subdomains except one in Nginx

I have an application with many urls like this:
dashboard.app.mycooldomain.com
subdomain-1.app.mycooldomain.com
subdomain-1.app.mycooldomain.com
subdomain-3.app.mycooldomain.com
subdomain-n.app.mycooldomain.com
and nginx config
server {
listen 5001 default;
listen [::]:5001;
server_name *.$hostname;
location / {
alias /usr/share/nginx/html/home/;
index index.html;
try_files $uri $uri/ index.html =404;
}
}
server {
listen 5001;
listen [::]:5001;
server_name dashboard.*$hostname;
location / {
alias /usr/share/nginx/html/dashboard/;
index index.html;
try_files $uri $uri/ index.html =404;
}
}
I expect when I visit dashboard.app.mycooldomain.com or dashboard.app.localhost nginx should serve all static file in /usr/share/nginx/html/home/ and when I subdomain-1.app.mycooldomain.com or subdomain-1.app.localhost or *.app.mycooldomain.com nginx should serve all static file in /usr/share/nginx/html/dashboard/. But now it does not work. How to write a config file correctly?
Create subdomains files in /etc/nginx/sites-enabled for all the required/different hosts/url, this way it would be much easier to manage the domains.

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

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.

Resources