Why nginx virtual host is not available from internet? - nginx

I made a virtual host with nginx and I've add to host:
127.0.0.1 testapp
the virtual host http://testapp is available on my VPS but via from internet is not accessible:
server {
listen 80;
server_name testapp testapp.51.x.x.172;
##root html;
root c:/apps/web;
index index.html index.htm;
}

You have DNS problem.
You had to define a CNAME record on your host like testapp FQDN will be testapp.yourdomain.com and target host should be your yourdomain.com which is already registered A record.
In your host you should add this line.
127.0.0.1 testapp.yourdomain.com
your problem was finished
But you probably need to done a proxy revers on your web server.

Related

Basic proxy_pass from nginx from one local ip to another local ip

I am a new user of nginx and I am following a video guide from Linode on youtube (How to Set Up an NGINX Reverse Proxy).
I have a working nginx and apache server both on port 80. I know that because when I type the ip address of both in firefox, it directs me to nginx/apache welcome page.
The youtube video configuration template is as follow (where the server_name is the linode ip) :
server {
listen 80;
listen [..]:80;
server_name 172.105.104.226;
location / {
proxy_pass http://localhost:3000/;
}
On my Proxmox machine, the nginx server is on a VM at 192.168.1.241 and the apache server on another VM at 192.168.1.243.
Looking at nginx documentation we find that this :
location /some/path/ {
proxy_pass http://www.example.com/link/;
}
should proxy all the traffic received on the nginx listening port and redirect it to the address specified by proxy pass.
With all these information, my configuration file is like this :
server {
listen 80;
listen [::]:80;
server_name 192.168.1.241;
location / {
proxy_pass http://192.168.1.243;
}
}
My understanding is that this configuration file should listen at the address 192.168.1.241 on port 80 (nginx server) and redirect it to the specified address 192.168.1.243 (apache server)/
If i understand correctly, Location / should take the request as is received on the nginx server and redirect it to the apache server.
However, when I enter 192.168.1.241 in my browser, it doesn't show the apache welcome message but shows the nginx welcome message. That means that the proxy isn't working.
My nginx understanding is extremely limited as I am just starting to learn, but to me it seems like this should work but doesn't.
Thank you for your help
It turns out that the configuration is correct.
The problem was that the webpage was cached. By forcing a full refresh, 192.168.1.241 redirected to 192.168.1.243 successfully.

nginx proxy_pass serving from default path other than application's

I'm running a local web service listening on port 8888
server {
listen 80;
server_name dracut.site;
location /jupyter {
proxy_pass http://127.0.0.1:8888/;
}
}
When I access http://dracut.site/jupyter, nginx is using files under /etc/nginx and returns error.
"/etc/nginx/html/tree" failed (2: No such file or directory)
Files should be served from the app listening on port 8888, how can I configure it.
your desciption is not complete...
you're using static html?
then want nginx to serve it...
just define root then location tag to that root

nginx + cloudflare + digitalocean = 521

I'm trying host a website with multiple subdomains (created with Cloudflare, which also provides SSL) hosted on DigitalOcean with Nginx serving as a reverse proxy.
My Cloudflare Configs
DNS setup:
Type ~ Name ~ Value
A ~ api ~ MyDigitalOceanIPv4
A ~ example.com ~ MyDigitalOceanIPv4
A ~ www ~ MyDigitalOceanIPv4
Crypto setup:
SSL: Full (strict)
Always use HTTPS: On
Automatic HTTPS Rewrites: On
I've also used Cloudflare to Create Certificate (and followed their instructions to set it up with Nginx)
My Nginx config:
server {
listen 443;
server_name example.com www.example.com;
ssl on;
ssl_certificate /srv/example.com/cloudflare.pem;
ssl_certificate_key /srv/example.com/cloudflare.key;
location / {
proxy_pass http://localhost:8000;
}
}
server {
listen 443;
server_name api.example.com;
ssl on;
ssl_certificate /srv/example.com/cloudflare.pem;
ssl_certificate_key /srv/example.com/cloudflare.key;
location / {
proxy_pass http://localhost:8080;
}
}
I have opened for all TCP ports on DigitalOcean, and if I try to open MyDigitalOceanIPv4:8000 in my browser then my website (hosted in a Docker container) successfully loads. However, if I try to open my website "example.com" then I get Cloudflare's 521 web server is down message.
I have also verified that the Cloudflare SSL key paths and content are correct, nginx -t shows no errors, and I've made sure to restart nginx after making changes.
I have also tried to whitelist Cloudflare's IPs using my Nginx config file but it didn't work.
If I try to telnet MyDigitalOceanIPv4 443 or 80 then I get telnet: Unable to connect to remote host: Connection refused.
Inside my DigitalOcean instance I have tried to curl http://localhost:8000 which successfully prints my website content.
I suspect there's some DigitalOcean setting I need to configure, or there's something wrong with my Nginx file (even though I've successfully used same Nginx config on a different cloud provider), but feel like I've tried everything..

How to use Nginx with different physical machines?

I have many VMs running with different web servers and such. I want to be able to use the SSL port on more than one machine, which is where Nginx comes in.
I have looked and dug, and I do not understand if you can achieve using different physical VMs with Nginx.
I have a:
CentOS machine running Apache as my main (no virtual hosts) website. IP of 10.40.1.12 internally, reachable at kodysalak.com
Windows Server 2016 with exchange on it. IP of 10.40.1.17 internally, reachable at mail.kodysalak.com
Windows Server 2016 with Spiceworks' HelpDesk software (Apache). IP of 10.40.1.14 internally, reachable at help.kodysalak.com
Those are the hosts that use 80/443. I have a separate CentOS machine running Nginx with no configuration done with it. IP of 10.40.1.18.
Any help would be SO helpful, of course.
Yes you can.
nginx's proxy_pass (reverse proxy) and server_name (vhost).
http://nginx.org/en/docs/http/server_names.html
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
Portforwarding from Firewall, then with the HTTP Header "Host" (SSL/TLS: SNI) aka virtual hosting.
Edit:
server {
listen 443 ssl;
server_name www.kodysalak.com kodysalak.com;
location / {
proxy_pass https://10.40.1.12;
}
}
server {
listen 443 ssl;
server_name mail.kodysalak.com owa.kodysalak.com;
location / {
proxy_pass https://10.40.1.17;
}
}
server {
listen 80;
server_name help.kodysalak.com;
location / {
proxy_pass http://10.40.1.14;
}
}

How to test nginx subdomains on localhost

I want to test nginx subdomains before uploading config to the server. Can i test it on localhost? I try
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8080;
}
}
server {
listen 80;
server_name sub.localhost;
location / {
proxy_pass http://localhost:8080/sub;
}
}
And it does not work. Shoulld i change my hosts file in order to make it work? Also, after uploading site to the server should i change DNS records and add sub.mydomain.com?
Yes, add '127.0.0.1 sub.localhost' to your hosts file. That sub has to be resolved somehow. That should work.
Then once you're ready to go to the net, yes, add an a or cname record for the subdomain sub.
When I use proxy_pass I also include the proxy.conf from nginx.
http://wiki.nginx.org/HttpProxyModule
In Linux based OS just to edit as sudo /etc/hosts file and change 127.0.0.1 localhost to 127.0.0.1 *.localhost.
So at /etc/nginx/sites-enabled/<environment>/<your_project_name> edit server_name key as <subdomain>.localhost.
Reload nginx and networking service.
$ sudo service nginx reload
$ sudo service networking reload
And then try http://<subdomain>.localhost at url bar.
It works for me.
UPDATE
In my opinion, a better solution is creating a virtual server that only responds if subdomain doesn’t exist, at /etc/nginx/sites-enabled/development/default, as default server (remember that you can define only one server as default).
server {
listen 80 default_server;
root /var/www/html/errors/404;
server_name *.localhost *.<host-name>;
location / {
index subdomain.html;
}
}
Make sure that in nginx.conf (generally at /etc/nginx/nginx.conf) contain include /etc/nginx/sites-enabled/**/*; to this virtual server work. If not, put it and then run $ sudo service nginx reload.
In this case isn't necessary put *.localhost in /etc/hosts, but only localhost.
For your public webserver with its own domain name, you just need to add a Canonical name using a CNAME record in your DNS configuration:
CNAME * example.com.
Once this is done, set your nginx setting
server_name *.example.com example.com;
In your local setup you can keep the same configuration for nginx but unless you have a local DNS setup, you will have to edit your /etc/hosts file and add each subdomain manually. wildcards don't work in the /etc/hosts file.
127.0.0.1 abc.example.com def.example.com ghi.example.com
It is generally recommended to use .local as the namespace for your local domains.
With an Nginx configuration like shown by the OP, all that is needed is to configure the local DNS resolution. I run Linux containers on a VM with a local DHCP IP but test them on Windows 10 browsers.
The DNS configuration can be done by editing "C:\Windows\System32\drivers\etc\hosts" as Administrator.
192.168.100.50 sub.example.local
192.168.100.50 example.local
Of course, use 127.0.0.1 or other appropriate IP as needed.

Resources