How to host 2 virtual hosts on nginx on www subdomains? - nginx

I am new to all this and trying hard already for 4 days without any big success. Maybe it's just a small thing and hopefully you can help me! Was not able to find an answer here so far (at least nothing worked out until now):
I have a domain, say example.com registered. I also added a subdomain test.example.com and added it as a CNAME to example.com.
Then I installed nginx and set it up following th tutorial on https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-lts with the minor change that I have both servers in one config file by now. In short, it looks like this:
in etc/nginx/sites-enabled/example.com
server {
listen 80;
root /var/www/example.com/html;
index index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
root /var/www/test.example.com/html;
index index.html index.htm;
server_name test.example.com www.test.example.com;
location / {
try_files $uri $uri/ =404;
}
}
example.com works fine and shows me "welcome to example.com" in broswer tab and some text that example.com server is running as expected (defined index.html by me as in the example).
BUT: If I go to test.example.com it shows me in the tab "welcome to example.com" either although it should be "welcome to **test.**example.com" and there is no html page at all shown to me. it's just a blank page.
Can someone help me? I don't know, if I already got the stuff with the cname domain or whether there might be something wrong.
Thank you very muh in advance!:-)

I was able to solve my problem. It was not on the server but a wrong setting in my domain provider. I don't have to forward my domain but just set it in the nameservers instead pointing to the ip!

Related

Nginx reverse proxy return 502

I'm very new to nginx and server game and i'm trying to setup a reverse proxy. Basically what i need is when i enter my server ip it should open a particular website (Ex: https://example.com).
So for example if i enter my ip (Ex: 45.10.127.942) it should open the website example.com , but the url should remain as http://45.10.127.942.
I tried to set my server configuration as follows but it returns a 502 error.
server {
listen 80;
location / {
proxy_pass http://example.com;
}
}
It returns a 502 error. Can you please explain what i need to do?
You can have something like this in your configuration file:
server {
root /var/www/html;
server_name _;
location / {
try_files $uri $uri/ /index.html;
}
}
Place the index.html file in root folder specified.
Then just restart the NGINX and it should work.
What is the problem with your configuration file is you should not proxy_pass.
If you want to open the other website, you should have DNS record pointing to that IP. What actually happens is the thing you are trying to do is known as CLICKJACKING. For more details, search CLICKJACKING on google and you will find a lot of references.

Nginx timeout only for subdomain

I have a problem with my Nginx configuration, I'm able to access a domain, but unable to access a "custom" subdomain of a given DNS.
www.myDomain.com => works perfectly
test.myDomain.com => works perfectly
test.myDomain2.com => ERR_CONNECTION_TIMED_OUT
I have an entry in my DNS for all the mentioned address for the same server IP, and when I ping them, they both works well.
www.myDomain2.com is a website an another domain, and I already have some subdomain redirected to other servers.
This is my config right now.
If I replace test.myDomain2.com by www.myDomain.com or test.myDomain2.com it's working (so link beetween sites-enable and available seems to be ok).
server {
listen 80;
listen [::]:80;
root /var/www/myDomain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name test.myDomain2.com;
location / {
try_files $uri $uri/ =404;
}
}
No SSL involved, it's the next step !
Pretty sure I'm missing something here, but I'm unable to find the solution... A little help will be really appreciated !
édit: more information, maybe not Nginx but DNS related, but since the pings are working I'm lost..

Nginx subfolder response inconsistent

I have a front-end application (in reactjs) and a static blog that I want to display from the same server through nginx. My application is accessible at the root of the domain / and my blog is currently accessible on a subdomain blog.domain.com. But I'm currently switching from a subdomain to the /blog subfolder on the same domain and I have an issue while displaying the blog from the subfolder. The problem only appears on chrome and is quite inconsistent. What happens is that when browsing /blog I get a 404 from the js application, not the static site (the css styles are different so it's easy to spot who responded). If I refresh the page without cache (ctrl + shift + R) I get a response from static blog. If I click on a link, again I get a 404 from the js page and if I clean-refresh now I get the correct page from the blog. Any f5 on a page redirect to a 404 from the js page.
In firefox or private navigation, everything seems fine though so I was thinking about a common cache issue in the browser at first but the fact that after dropping the cache I still get 404s from the wrong directory makes me think about some parameters that would be sent and not treated the same way in one case or another.
I have noticed that ctrl-shift-R redirects me to /blog/ from /blog when the blog is displayed properly but an f5 on /blog/ still gives a 404 from the js frontend. It might mean that the 301 to the trailing slash does something more. But a ctrl-shift-R on /blog/ does not trigger a 301 (just a 200) with the expected website displayed.
Here is my nginx config file for this site:
server {
listen 443 http2 default_server;
listen [::]:443 http2 default_server;
root /opt/react_folder/build;
index index.html;
server_name domain.com www.domain.com;
location / {
try_files $uri $uri/ /index.html =404;
}
location /blog {
alias /opt/blog_subfolder;
try_files $uri $uri/ /index.html =404;
}
... backend block
... ssl block
}
server {
listen 0.0.0.0:80;
server_name domain.com www.domain.com;
rewrite ^ https://$host$request_uri? permanent;
}

Nginx - Create Multiple Sub Domains

I would like to create multiple subdomains using nginx from 1 IP address. So it would be something like this:
http://demo1.192.168.0.27
http://demo2.192.168.0.27
Someone already asked this question in the past.
nginx - two subdomain configuration
I tried the same way but I'm not able to do it.
Here's my code:
events {
}
http {
server {
server_name demo1.192.167.0.27;
root /data/sites/demo1;
index index.html;
location / {
try_files $uri $uri/ /404.html;
}
}
server {
server_name demo2.192.167.0.27;
root /data/sites/demo2;
index index.html;
location / {
try_files $uri $uri/ /404.html;
}
}
}
When I go to
http://demo1.192.168.0.27
http://demo2.192.168.0.27
It said, This site can’t be reached
Not sure why it's not working for me.
You can't create subdomains on IP addresses.
In the answer you're referencing, they're using domain names, like this:
server_name sub1.example.com;
server_name sub2.example.com;
That's why it works, as opposed to what you have, with IP addresses:
server_name demo1.192.167.0.27;
server_name demo2.192.167.0.27;
Your domain names are not public. You can resolve to a DNS provider to make them public.
You also can add xxx.xxx.xxx.xxx demo1.192.168.0.27 to your local hosts file C:\Windows\System32\drivers\etc\hosts, to make them avaliable to your local machine.

Redirect nginx /index.html to root causing infinite redirects

I want to avoid having the same HTML page accessible on www.example.com and www.example.com/index.html, i.e i want to redirect the index.html to root.
This:
location = /index.html {
return 301 $scheme://www.example.com;
}
is causing me to get ERR_TOO_MANY_REDIRECTS, a redirect loop.
Any ideas what can i change to make it work?
PS this is my entire nginx conf
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include /etc/nginx/snippets/ssl-example.com.conf;
include /etc/nginx/snippets/ssl-params.conf;
include /etc/nginx/snippets/letsencrypt-challenge.conf;
root /var/www/newsite;
index index.php index.html index.htm;
server_name www.example.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location = /index.html {
return 301 $scheme://www.example.com;
}
}
This is not a detailed treatment of the subject but a simplified explanation just to answer your dilemma. The answer is that you need to abandon the attempt to do what you are doing.
Webservers can only serve specific files such as an xyz.html file. They cannot serve folders.
A call to https://www.example.com/abc/index.html is a request for the index.html file in the abc folder of the web root. A call to https://www.example.com/abc on the other hand is a request for the abc folder of the web root, which as mentioned, cannot be served.
As you have noticed however, the second call results in https://www.example.com/abc/index.html being served. This is because webservers are generally set up such that when a call is made to a folder without specifying a specific file to serve, a redirect to the index.html file in that folder is generated and this is served instead. That is, the webserver internally turns the request for https://www.example.com/abc into a request for https://www.example.com/abc/index.html.
This is what the index index.php index.html index.htm; line in your config does. It says "if there is a request for a folder without a file specified, serve the index.php file instead. If there is not such file, serve the index.html. If there is not such file, serve the index.htm file. If this also does not exist, throw a fit"
The problem is that you then go on to instruct your webserver to redirect requests for https://www.example.com/index.html to https://www.example.com which the webserver redirects back to https://www.example.com/index.html which is again redirected back to https://www.example.com in an endless loop until the webserver or your browser finally gives up.
You stated that I want to avoid having the same HTML page accessible on www.example.com and www.example.com/index.html, i.e i want to redirect the index.html to root. The question is why? There is absolutely no benefit in doing this and as you found out, you end up in a redirect loop.
You may be trying some SEO stuff but this does not apply here.

Resources