Accept all subdomains except one in Nginx - 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.

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.

How do I add text to a response from a remote URL in NGINX?

I have the following server in NGINX and it works fine. But, I am wondering is it possible to add text to a response from a remote URL where hosts my before_body.txt and after_body.txt? Is there any way to tackle this?
server {
listen 80;
root /storage/path;
index index.html;
server_name test.domain.com;
location / {
try_files $uri $uri/ =404;
add_before_body /src/before_body.txt;
add_after_body /src/after_body.txt;
autoindex on;
}
location /src/ {
alias /storage/path/content/;
}
}
I have resolved with replacing the alias as follows:
location /src/ {
proxy_pass https://externalserver.com/;
}

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 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.

Configuring multiple sites on nginx server

I would like to configure multiple websites on my nginx server. My configuration under /etc/nginx/sites-enabled/default is as below :
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html/MyWebsite;
location / {
try_files $uri $uri/ =404;
}
location /admin {
alias /var/www/html/MyWebsiteAdmin;
try_files $uri $uri/ =404;
}
}
My website is deployed at www.mywebsite.com. I am able to access www.mywebsite.com but not able to access www.mywebsite.com/admin due to few errors like, it cannot load js and css files eg: www.mywebsite.com/inline.bundle.js, www.mywebsite.com/theme.css etc. So, It is trying to access js and css files from www.mywebsite.com but not from www.mywebsite.com/admin/inline.bundle.js. How can i resolve this issue?
/admin are folder then you not need
location /admin {alias /var/www/html/MyWebsiteAdmin;try_files $uri $uri/ =404;}
now /admin is like www.mywebsite.com/index.php/admin then you have to add rewrite rule of index.php
location / {index index.html index.php;try_files $uri $uri/ #handler;}location #handler { rewrite / /index.php; }

Resources