Nginx subdomains not working - nginx

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.

Related

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.

Nginx and relative paths for proxied subdomain

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

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.

Subdomain not working with Nginx virtual host

I have setup subdomain virtual host but its not working getting 522 error.
main domain is working fine. I have also check nginx error log but its not showing any error.
Envirnment
Ubantu 18.10
I have already setup DNS and also check nginx error log but don't see any nginx errors.
server {
listen 80;
listen [::]:80;
server_name subdomain.com www.subdomain.com;
root /var/www/html/subdomain.com/public_html;
error_log /var/log/nginx/error.log;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
anyone please suggest possible solution to fix this issue.
Thanks

How can I mask a Nginx virtual host redirect?

I was asked to make a rewrite rule to redirect a virtual host subdomain to another virtual host managed by the same Nginx server. That works fine but I'd like to mask the URL redirect in the browser.
There is a configuration file for each virtual host: subdomain.mydomain.com.conf configuration file was made from original www.mydomain.com.conf with required substitutions.
This is www.mydomain.com.conf virtual host configuration file:
server {
listen 127.0.0.1:80;
listen 443 ssl;
server_name www.mydomain.com;
access_log /var/log/nginx/www.mydomain.com-access_log main;
error_log /var/log/nginx/www.mydomain.com-error_log;
root /var/www/html/www.mydomain.com;
index index.php index.html index.htm;
...
location / {
try_files $uri $uri/ /index.php?$args;
}
...
I made a Nginx rewrite rule to redirect URLs beginning with www.mydomain.com/subdomain to subdomain.mydomain.com:
location /subdomain {
rewrite ^/$ subdomain.mydomain.com$request_uri last;
}
I found a link that explains how to make an internal redirect :
http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.VFKqiYXXqPK
I made several attempts but the redirect is always shown.
As I'm new to Nginx, can somebody help me out ?
best regards,

Resources