How to serve multiple web servers using nginx? - nginx

I have installed nginx and I want to serve two different web applications under the same user on the same server.
This is the config I already use:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
return 301 https://www.example.com$request_uri;
}
# HTTPS — proxy all requests to the Node app
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Vary Accept-Encoding;
access_log off;
}
root /home/myuser/main/dist;
# Use the Let’s Encrypt certificates
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
}
As you can see, I have under /home/myuser directory a directory called main and a dist directory in it. There there are static files that are successfully served using nginx.
I want to add another directory under the myuser directory, called for example, test.
So I will have /myuser/test and there to server another web application. Using the very same nginx server.
I have tried to write many variants in the config file I mentioned above but it couldn't work.
The config file located in: /etc/nginx/sites-enabled/example.com.conf
I edit it using sudo.

If you want to host different static files from local directories a configuration could look like this:
Notice: Your location uri (/, /one) will be appended to the root directory path if using the root directive.
The root directive can be used in every location block to set the document root.
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
This is the reason why alias exists. With alias the location will not become a part of the directory path. Check this out:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
1. One Domain - Multi-Location
server {
server_name example.com;
listen 443 ssl;
.....
root /home/user/main/dist;
location / {
index index.html;
# If you have some sort of React or Angular App you might want to use this
# try_files $uri $uri/ /index.html;
# If you just host a local files (css, js, html, png)...
# try_files $uri $uri/ =404;
}
location /two {
alias /home/main/example;
index index.html;
.....
}
}
2. Two Domains - Single Location
server {
server_name example.com;
listen 443 ssl;
.....
root /home/user/main/dist;
location / {
index index.html;
# If you have some sort of React or Angular App you might want to use this
# try_files $uri $uri/ /index.html;
# If you just host a local files (css, js, html, png)...
# try_files $uri $uri/ =404;
}
}
server {
server_name example1.com;
listen 443 ssl;
.....
root /home/user/main/test;
location / {
index index.html;
# If you have some sort of React or Angular App you might want to use this
# try_files $uri $uri/ /index.html;
# If you just host a local files (css, js, html, png)...
# try_files $uri $uri/ =404;
}
}

Related

Can't serve two static websites with Nginx

I have two React build folders and I want to access them like this :
https://example.com/ -> /var/www/web_client/
https://example.com/videochat/ -> /var/www/videochat/
Here is my nginx configuration file :
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
location / {
root /var/www/web_client;
try_files $uri /index.html;
}
location /videochat/ {
root /var/www/videochat;
try_files $uri /index.html;
}
listen [::]:443 ssl ipv6only=on;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
The location block / works perfectly but /videochat/ redirects me to the 404 page of /var/www/web_client and not the index.html of /var/www/videochat.
How can I fix this error and get nginx to serve both static websites on the same server ?
I tried to reverse the two like this to make sure the error is coming from nginx:
location / {
root /var/www/videochat;
try_files $uri /index.html;
}
location /web_client/ {
root /var/www/web_client;
try_files $uri /index.html;
}
Now it's the other way around, the location block / redirects me to the index.html of /var/www/videochat as expected but /web_client/ redirects me to /var/www/videochat and not the index.html of /var/www/web_client.
I think you can try having /videochat location block first and then then the / location block.
Because location /, will be the block that gets used for both / as well /videochat.
Only if we specify '=' in the location match, it will be a exact match, if not requested URI will be matched against the beginning of the mentioned URI in the location block.
For many location blocks it will be better to use regex.
You can refer to article like this for more info:
https://www.digitalocean.com/community/tutorials/nginx-location-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 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.

Nginx route with single page app

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

Nginx, Basic_Auth for directory listing only (not files, sub folders)

anyone know of a way for NGINX to apply basic authentication to the directory listing (root directory in my case) but not to the invidiual files and subfolders within that root folder?
For example: if I accessed http://example.com I would be presented with basic authentication
but if I accessed http://example.com/audio.mp3 I would be able to access it without basic authentication on (publicly available)
Here is a sample of my nginx block
server {
listen 443 ssl;
server_name example.com;
# Configure SSL
ssl_certificate /etc/ssl/certs/nginx/ssl.cert;
ssl_certificate_key /etc/ssl/certs/nginx/ssl.key;
include /etc/nginx/includes/ssl.conf;
location ~ /$ {
auth_basic "Admin Access";
auth_basic_user_file /etc/ssl/certs/nginx/.htpasswd;
include /etc/nginx/includes/proxy.conf;
proxy_pass http://111.111.111.111;
autoindex on;
}
location / {
try_files $uri =404;
}
access_log off;
error_log /var/log/nginx/error.log error;
}
Thank you

Resources