Nginx and relative paths for proxied subdomain - nginx

I'm a newbie in Nginx, trying to learn.
I have the server under mydomain.com and my static site under my-app.mydomain.com
All the paths are relative, so images/image.png resolves to my-app.mydomain.com/images/image.png.
I also have a second app, new-app.mydomain.com which has the same issue, the relative paths are trying to be resolved to mydomain.com
I don't know how to fix this and I would like to avoid having to make all paths absolute. Also, I would like a solution that allows me to keep adding new locations blocks for the new app and load the resources. I want to avoid some restrictive that could work for the main app but not for the other.
location /new-app {
proxy_ssl_server_name on;
proxy_pass "mydomain.com";
}
I will appreciate help.

Locations inside a server can help you configure website/content that need to be displayed on sub-routes of website. Sub-domains need to be configured in a seperate nginx file similar to main domain in which you can add as many location as per your requirement.
Nginx files:-
mydomain.com
server{
listen 80 default_server;
listen [::]:80 default_server;
server_name mydomain.com;
location / {
proxy_ssl_server_name on;
proxy_pass "mydomain.com"; # This should be server with port on which your server is running on VM(virtual machine)
root /var/www/html/mydomain.com; #In case you want to server static files
try_files $uri $uri/ /index.html;
}
}
new-app.mydomain.com
server{
listen 80 default_server;
listen [::]:80 default_server;
server_name new-app.mydomain.com;
location / {
proxy_ssl_server_name on;
proxy_pass "new-app.mydomain.com"; # This should be server with port on which your server is running on VM(virtual machine)
root /var/www/html/new-app.mydomain.com; #In case you want to server static files
try_files $uri $uri/ /index.html;
}
}

Related

Change port for http to https -- Nginx

Sorry for limited understanding on Nginx, Iam new to Nginx.
I have a webapp running on React and Nginx. Recently I received the SSL certificates for my website. I tried to configure the website and it worked partially. The problem is when I tried to open "https://example.com", the SSL certificates are visible here but its showing nginx default home page. While when I open "http://example.com" it shows all the webcontent.
I attempted to:
change the port from 80 to 443
Reinstall nginx.
But nothing seems to work. Here is my nginx confs at the moment:
/etc/nginx/sites-available/example.org
server {
listen 443;
listen [::]:443;
ssl on;
ssl_certificate /etc/nginx/ssl/bundle.cer;
ssl_certificate_key /etc/nginx/ssl/example.key
root /var/www/html;
server_name example.org;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
listen [::]:80;
server_name _;
return 301 https://example.org;
}
/etc/nginx/conf.d/www/example.org.conf
server {
listen 80 default_server;
server www.example.org;
location / {
root /usr/share/nginx/html;
index index.htm; index.html;
}
}
Note: I reload nginx at every new attempt.
Please help where am I going wrong.
Keeping just 1 file for config works for the above problem. I kept the "default" conf at "/etc/nginx/sites-available"

simple Nginx configuration on t2 (AWS EC2) server gives 504 Gateway Time-out on subdirectories

I have a t2 server (AWS EC2 server) that I want to simply serve an HTML file as the root:
http://my_ip/ -> gives me my index.html
and another one serving a reverse_proxy
http://my_ip/api -> gives me my "API" (or whatever runs on the reverse proxy IP)
So I configured the Nginx as the followings:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_pass http://127.0.0.1:3000;
}
}
Simple enough, in this configuration, my index.html is showing up. But whenever I'm trying to access the "reverse proxy" (/api), I get 504 Gateway Time-out error.
So I tried to "switch places", I gave the reverse-proxy the "root" / and the index.html the "/api".
Getting this configuration:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000;
}
location /api {
try_files $uri $uri/ =404;
}
}
This way, the reverse proxy is doing OK! (from /), and when I try to reach index.html (from /api), I'm getting 504 Gateway Time-out error.
So I figured that something is wrong with the "sub-directories" configuration. I tried such configurations on many servers before (mainly from DigitalOcean and others) and never ran into something like that.
My guess is it got something to do with the t2 (EC2) NAT configuration (maybe?). But I'm not so sure.
Googling the issue didn't raise any answers.
There's Ubunutu 20.04 installed on the t2 server, It's "brand new" (nothing was installed but Nginx, Node & NPM - was done by me, "by the book" installations with no needed configurations or file changing).
Note that I don't have a domain yet, I was thinking of trying to server the reverse proxy on a subdomain but I have no way to do it at this point, that's why I tried to do it via a subdirectory.
Thanks.

Reverse proxy nginx to itself

I am currently hosting a single-page react app that is hosted in the URL root like so:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I need to put the site behind an AWS elastic load balancer and at the same time change the path so everything is within a /support directory e.g. http://example.com/index.html -> http://example.com/support/index.html.
AWS ALBs do not support URL rewriting so I have to do this within the nginx config on the server. First of all I tried changing the config to:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
This sort-of works but the URLs within the javascript content don't contain the /support path (e.g. they contain http://example.com/script.js instead of http://example.com/support/script.js).
I then tried creating a reverse-proxy config to proxy /support to /, which sadly put nginx in an infinite loop until it ran out of worker threads:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
I'm confused why requests are going into a reverse-proxy loop? Shouldn't proxy_pass remove the /support prefix before proxying the request, and therefore it shouldn't be "caught" again by the /support location?
Just a guess.
Do you want to serve something on /?
If not - it is easy:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
Fiddle around with the ending slashes if it does not work (using them - or not - makes often a difference).
Use alias instead of root so that /support is not added to the /var/www/html folder.
Everything gets redirected to /support.
If you want to serve something on / which is different from /support:
Use sub_filter or subs_filter in /support to rewrite your source code links on-the-fly so that they will never use /.
If you have redirects inside your source code (or proxy_pass backend) - you need proxy_redirect and/or Lua to catch and change them on-the-fly.

Nginx subdomains not working

I don't know what's wrong. I don't get any warnings in logs. I've similar config to this
How to exclude specific subdomains server_name in nginx configuration
I want to create subdomain us.example.io I'm using ping to check it
ping us.example.io
ping: cannot resolve us.example.io: Unknown host
nginx.config
server {
server_name *. us.example.io us.example.io;
listen 80;
root /usr/share/nginx/html/bint;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
server_name us.example.io;
listen 80;
return http://www.google.com;
}
The problem has nothing to do with nginx. The error suggests that you haven't configured a DNS record for the domain.

nginx - two subdomain configuration

I'm new to Nginx and I'm trying to get subdomains working.
What I would like to do is take my domain (let's call it example.com) and add:
sub1.example.com,
sub2.example.com, and also have
www.example.com available.
I know how to do this with Apache, but Nginx is being a real head scratcher.
I'm running Debian 6.
My current /etc/nginx/sites-enabled/example.com:
server {
server_name www.example.com example.com;
access_log /srv/www/www.example.com/logs/access.log;
error_log /srv/www/www.example.com/logs/error.log;
root /srv/www/www.example.com/public_html;
location / {
index index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}
}
It is working to serve example.com and www.example.com.
I have tried to add a second server block in the same file like:
server {
server_name www.example.com example.com;
access_log /srv/www/www.example.com/logs/access.log;
error_log /srv/www/www.example.com/logs/error.log;
root /srv/www/www.example.com/public_html;
server {
server_name sub1.example.com;
access_log /srv/www/example.com/logs/sub1-access.log;
error_log /srv/www/example.com/logs/sub1-error.log;
root /srv/www/example.com/sub1;
}
location / {
index index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/www.example.com/public_html$fastcgi_script_name;
}
}
No luck. Any ideas? I'd super appreciate any feedback.
The mistake is putting a server block inside a server block, you should close the main server block then open a new one for the sub domains
server {
server_name example.com;
# the rest of the config
}
server {
server_name sub1.example.com;
# sub1 config
}
server {
server_name sub2.example.com;
# sub2 config
}
You just need to add the following line in place of your server_name
server_name xyz.com *.xyz.com;
And restart Nginx. That's it.
Add A field for each in DNS provider with sub1.example.com and sub2.example.com
Set the servers. Keep example.com at last
As below
server {
server_name sub1.example.com;
# sub1 config
}
server {
server_name sub2.example.com;
# sub2 config
}
server {
server_name example.com;
# the rest of the config
}
Restart Nginx
sudo systemctrl restart nginx
You'll have to create another nginx config file with a serverblock for your subdomain. Like so:
/etc/nginx/sites-enabled/subdomain.example.com
There is a very customizable solution, depending on your server implementation:
Two (or more) SUBdomains in a single nginx "sites" file? Good if you own a wildcard TLS certificate, thus want to maintain ONE nginx config file. All using same services BUT different ports? (Think of different app versions running concurrently, each listening locally to differents ports)
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ~^(?<sub>.+).example.com;
# default app (the "default" ports, good for the "old" app)
set $app 19069;
set $app-chat 19072;
# new app
# new.example.com
if ( $sub = "new" ) {
set $app 18069;
set $app-chat 18072;
}
# upstreaming
location / {
proxy_redirect off;
proxy_pass http://127.0.0.1:$app;
}
location /longpolling {
proxy_pass http://127.0.0.1:$app-chat;
}
I know the performance will "suck", but then again, since the decision was to go with one server for all it's like complaining that an econobox cannot haul as much people as a bus because the little car has a "heavy" roof rack on top of it.
A regex expert could potentially improve the performance for such a custom solution, specially since it could ommit the CPU expensive "if" conditional statements.
Maybe this could help someone having a challenge with this, this got me grinding the whole day.
First, if you have SSL installed, remove it (delete better still), this would help reset the previous configuration that's disrupting the subdomain configuration.
in etc/nginx/.../default file
create a new server block
server {
listen 80;
server_name subdomain.domain.com;
location /{
proxy_pass http://localhost:$port;
}
}

Resources