Configure Nginx subdomains for different paths - nginx

I want to configure subdomains, so that it can accept all subdomains except admin. Currently admin is handled by other nginx configuration.
For example,I want to have ru.example.com and en.example.com to navigate to same website without redirection,and admin.example.com to navigate another website without redirection.
Here is my configuration file.
server {
listen 80;
server_name example.com www.example.com;
location / {
root /srv/www/example.com;
try_files $uri $uri/ /index.html;
}
}

Related

How to configure Nginx for URL mapping of CNAME requests?

I'm trying to configure my Nginx server block to accept requests without changing the requested domain, so that my users can use their custom domain with my application via a DNS CNAME-type entry.
The expected behaviour is as follows:
User creates a dns cname entry pointing to my server/domain e.g. www.userdomain.com to www.mydomain.com
My server then directs the request to my application www.mydomain.com/app while maintaining the URL www.userdomain.com
I've been able to make this behaviour work as above by setting my www.mydomain.com server block as default_server, but i'm trying to find a different way because i want to replicate this behaviour with a different server block/domain in the same server.
My current Server Block looks like the example below:
server {
listen 80 default_server;
root /var/www/mydomain.com/app;
index index.php;
server_name www.mydomain.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
location ~ /(.*)$ {
try_files $uri $uri/ /;
}
}
Is it possible to reproduce this behaviour without setting the default_server flag?

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.

EC2 - can ssh but cannot access to a web page server by Nginx

I setup my ECS instance with my own VPC, with its own Security Group (:22/:80/:443 open).
Gateway everything is working.
I can ssh to my server with the public IP.
But I cannot serve a simple HTML page with Nginx. I use the config I had on my own server but I cannot reach my page.
Nothing fancy in my Nginx config :
server {
listen 80;
listen [::]:80;
server_name www.gfelot.xyz gfelot.xyz;
root /var/www/siteWeb;
index index.html;
location / {
try_files $uri $uri/ =404;
}
# location ~ /.well-known {
# allow all;
# }
}
Any idea why ?
I suggest you check firewall rules and SELinux setting on VPC server.

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.

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