How to configure subdomains in nginx - nginx

I am trying to load another website into my subdomain using reverse proxy in Nginx
for example
www.example.com is my website
test.example.com is my subdomain
and I want to load www.facebook.com into the subdomain
Now if the user enters test.example.com Facebook will be loaded into this subdomain and I also have access to use its content, like I will be able to getelementbytag etc.
Please tell me how to do that, with proper steps because I am new in Nginx. Thanks

Here I just took this from one of my projects. It demonstrated that the server_name you specify in each server block will be matched against the host header in the incoming request.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
# my domain
server_name mydomain.dev;
root /usr/share/nginx/html;
include /etc/nginx/snippets/ssl.conf;
location / {
root /usr/share/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
server {
listen 443 ssl http2 ;
listen [::]:443 ssl http2;
# my sub domain
server_name foobar.mydomain.dev;
location / {
proxy_pass https://foobarbaz.website-eu-central-1.linodeobjects.com/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Related

why is nginx not serving content when i redirect?

so, i got the webserver to redirect to my domain name when i type in the droplet ip address into the browser. but it wont serve any of the content that was being served before i tried this.
how do i get it to both redirect to the domain.com AND show the content?
server {
listen 80;
listen [::]:80;
server_name digital.ocean.ip.address;
return 302 http://**mydomain**.com$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name http://**mydomain**.com;
location / {
root /usr/share/nginx/html;
index hm.html hm.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
i should mention i am running nginx in a container 0.0.0.0:8081:80
(with port forwarding)
i've updated to this, because a single server block didn't work:
server {
listen 80 default_server;
listen [::]:80;
server_name _;
return 301 domain.com$request_uri;
}
server {
server_name domain.com;
location / {
root /usr/share/nginx/html;
index hm.html hm.htm;
}
}
and now im gettin getting:
414 Request-URI Too Large
with the domain.com/domain.com/domain.com/ repeating
when i type www.domain.com it takes me to the correct digital ocean ip address - so i believe dns is good.
so, i'm still unable to serve content, unless i dont redirect. in which case if i type the ip address in, that's what shows on the browser.

Nginx HTTPS (443) config ignored and HTTP (80) used instead

I was trying to redirect non secure (domain.com and www.domain.com) to secure version and I was getting a "too many redirects" error.
So, I decided to simplify the config to test and try to find out the error.
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name example.com www.example.com;
return 302 https://www.google.com;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2 ipv6only=on;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/cert.key;
server_name example.com www.example.com;
return 302 https://www.amazon.com;
}
If I am not wrong, when I visit http://example.com/ or http://www.example.com/, I should be redirected to https://www.google.com
And I if I visit https://example.com/ or https://www.example.com/, I should be redirected to https://www.amazon.com
But, any case, I am always redirected to https://www.google.com. What is wrong?
Your browser might be caching the redirects.
Try using Incognito windows for both the test cases. Your config file seems to be fine.
For your domain.com, you can have following configuration:
server {
server_name _;
listen 443;
root /var/www/html;
location / {
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}

NGINX + CentOS 8 mydomain.com shows NGINX welcome page whereas mydomain.com/index.html shows website index page

I'm trying to configure NGINX on CentOS 8 (with SSL). I believe I have configured it properly, but I can't get rid of the NGINX welcome page. I've tried a lot of things, even deleted the entire /usr/share/nginx/html directory, but I still get NGINX welcome on example.com, whereas example.com/index.html gives me the index page of my website. In fact I have noticed that the http to https and non-www to www redirections I have implemented below don't work on example.com, but do work on example.com/index.html. The root of my website is /var/www/example.com/html. The configure file which is given below is called example.com.conf and located at /etc/nginx/conf.d/.
server {
root /var/www/example.com/html;
index index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
listen [::]:443 ssl ipv6only=on default_server; # managed by Certbot
listen 443 ssl default_server; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# Redirect non-SSL to SSL
server {
listen 80;
listen [::]:80;
server_name .example.com;
return 301 https://$host$request_uri;
}
# Redirect non-www to www
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
So it appears that you need to edit the global configuration file using sudo nano /etc/nginx/nginx.conf and comment out "default_server" in the listen statements. Alternatively deleting the whole server block in the global conf file also works, as long as you keep the include statement which reads the example.com.conf file.
include /etc/nginx/conf.d/*.conf;
server {
listen 80; # default_server;
listen [::]:80; # default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

Forward nginx 8080 port to 80

I have a server with multiple nginx instances
One of them is running at 8080
server {
listen 8080;
server_name myip v2.example.com www.v2.example.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://v2.example.com:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
But I want to enter the site using example.com , instead of example.com:8080
Problem is that there is another server running at port 80 , I tried to fix it with the proxy pass, but it doesn't seem to work
How can I fix that?
You can try with ssl port 443. Of course you may need to add a valid ssl certificate to avoid error messages from the browser.
If you do not want to use the above method, you can use the proxy pass on one of your other instances as follows:
location /some-url/ {
proxy_pass http://localhost:8080/;
}
After that, You can then access it through: http://localhost:80/some-url/
Redirect with return. You can put the following in one file:
server {
listen 80;
listen [::]:80;
server_name v2.example.com www.v2.example.com;
return 301 http://$host$request_uri:8080;
}
server {
listen 8080;
server_name myip v2.example.com www.v2.example.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

Pass unfiltered requests for a specific endpoint

I have an Nginx setup whereby I included a file with whitelisted IPs that can access my site's admin portal admin.site.com. Usually all users are redirected to to site.com if the response is 403 or 404 i.e. if their IP is not in the whitelist. Here is what I have
server {
# listen on port 80 (http)
listen 80;
server_name admin.site.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/.well-known/acme-challenge/;
default_type "text/plain";
try_files $uri =404;
}
location / {
# redirect any requests to the same URL but on https
return 301 https://$host$request_uri;
}
}
server {
# listen on port 443 (https)
listen 443 http2 ssl;
server_name admin.site.com;
root /var/www/admin.site.com/site-frontend/;
index index.html;
location / {
include /etc/nginx/snippets/whitelist.conf;
error_page 403 404 =301 site.com;
}
# location of SSL certificate
ssl_certificate /etc/some/path/admin.site.com/fullchain.pem;
ssl_certificate_key /etc/some/path/admin.site.com/privkey.pem;
...
...
...
How do I allow unfiltered traffic to certain endpoints e.g. site.com/api/no-filters for all traffic?

Resources