Basic NGINX proxy_pass example is serving 404's - nginx

I have an very basic out-of-the-box NGINX server running on a Windows machine at http://10.0.15.19:80. I'm attempting to use it as a proxy to get around a CORS restriction. I have the following nginx.conf, which from every example I've seen sounds like it should redirect any traffic from http://10.0.15.19 to http://10.0.1.2:3000.
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name 10.0.15.19;
location / {
proxy_pass http://10.0.1.2:3000/;
}
}
}
Instead of serving the content from http://10.0.1.2:3000 I get the default index.html page inside the included html folder. Similarly, if I were to go to http://10.0.15.19/any/path I get a 404 even though http://10.0.1.2:3000/any/path works correctly.
EDIT: I've noticed that even after commenting out the entire server block of my configuration, it's still serving content from the included html folder. This makes me think there is another server configuration running that I'm not aware of, but I'm not sure where. I downloaded NGINX from here, and I assume all configuration files exist within this folder.

It turns out this was because simply closing the window that pops up when you open nginx.exe doesn't actually kill the process. And in Windows you can apparently have multiple services bound to the same port, so I had many servers serving on port 80. Killing all of these processes and relaunching with the originally posted config fixed my problem.

Related

Use Nginx to proxy to multiple sites under one domain name

I have a jenkins and sonarqube container running on a server. Is it possible to use Nginx to connect to each under the same domain name? So for example my.domain.com/jenkins hits the jenkins container and my.domain.com/sonar hits sonarqube?
My initial guess at the setup is something like this.
server {
listen 80;
server_name my.domain.com;
location /sonar {
proxy_pass http://sonarqube:9000/;
}
location /jenkins {
proxy_pass http://jenkins:8080/;
}
}
This issues I keep running into involve the subsequent calls made after the initial page. Is there a way to keep the /sonar/ and /jenkins/ piece in all the calls made?
You need to make those applications aware of the different context, so that they generate links correctly. For Jenkins you need to specify --prefix=/jenkins when starting Jenkins, for SonarQube you need to set up environment variable SONAR_WEB_CONTEXT=/sonar when starting SonarQube.
See:
https://www.jenkins.io/doc/book/installing/initial-settings/
https://docs.sonarqube.org/latest/setup/environment-variables/#header-2

How do I easily access all subfolders and files within an nginx reverse proxy server?

I'm not sure if I can even describe what I want/my current problem because honestly, I don't know exactly what I am doing. currently I have arenu.com.br going to a nginx server reverse proxying to port 8080. Here's what I did on my server: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
I have a simple node express hello.js file listening at port 8080 like this:
const express = require('express')
const app = express()
app.listen(8080, () => console.log('listening at 8080'))
app.use(express.static('public'))
inside my 'public' folder there is an index.html page which is what you see when you go to the website.
The only way I found how to make other subdomains point to other static files is through the nginx settings like this:
server{
location /snakegame/ {
proxy_pass http://localhost:8080/snakegame/index.html;
}
}
Here I am accessing another index.html file located at /public/snakegame/index.html by proxying arenu.com.br/snakegame to it. This setup however, is not versatile at all and I have to manually add every single subfolder to the settings everytime, and multiple problems occur. One of the problems as an example is this:
While accessing directly my server at port 8080: http://142.93.144.178:8080/snakegame/index.html the javascript on that page works perfectly (use arrow keys to control snake). But when accessing the same file using this reverse proxy method, the javascript does not work: https://arenu.com.br/snakegame/ (you can even look at the console to see that it can't access the js file)
Is there a better way for nginx to access multiple subfolders and static content easily? What should I do?
I believe I had the same Kinda problem just a few days ago, there is little documentation about this as most people prefer to define their subfolders manually in the Nginx server config. In my case, I was using LXD containers and needed to pass a domain through the hypervisor to the LXD container. In order to do this I had to change the config for the Nginx hypervisor container (proxy_pass):
location /snakegame/ {
proxy_pass http://127.0.0.0:8080/snakegame/;
try_files $uri $uri/ #backend;
}
location #backend {
proxy_pass http://127.0.0.0:8080/snakegame/;
}
I have changed the IP to match your requirments. I belive what happens is any request to the main index in your case index.html is forwarded but then the rest is sent to the Nginx proxy. the try_files and #backend seems to fix this. I'm not going to claim I know why :).

Using Proxy server to switch between Golang Applications

I have a server with CentOS, and there I will have at least 4 Golang applications running, every one of them is a different site that I should be able to access in the browser with domain/subdomains as follows:
dev00.mysite.com
dev01.mysite.com
dev02.mysite.com
dev03.mysite.com
So, I need to configure some kind of software that redirects the requests to the correct Golang process. Every site will be running in a different port, so for example if someone calls dev00.mysite.com I should be able to send that request to the process of dev00 site (this is for development porpouses, not production). So, here I'm starting to believe that I need Nginx or Caddy as I read, but I have no experience with none of them.
Can someone confirm that this is the way to fix that problem? and where can I find some example of configuration of any of that servers redirecting to Golang applications?
And, in the future if a have a lot (really a lot) of domains running in the same server, which of that servers is better? who is better with high load?
Yes, Nginx can solve your problem:
Start a web server using the standard library of Go or Caddy.
Redirect request to Go application using Nginx:
Example Nginx configuration:
server {
listen 80;
server_name dev00.mysite.com;
...
location / {
proxy_pass http://localhost:8000;
...
}
}
server {
listen 80;
server_name dev01.mysite.com;
...
location / {
proxy_pass http://localhost:8001;
...
}
}

Domain name and port based proxy

I think I finally grasped how Docker works, so I am getting ready for the next step: cramming a whole bunch of unrelated applications into a single server with a single public IP. Say, for example, that I have a number of legacy Apache2-VHost-based web-sites, so the best I could figure was to run a LAMP container to replicate the current situation, and improve later. For argument sake, here is what I have a container at 172.17.0.2:80 that serves
http://www.foo.com
http://blog.foo.com
http://www.bar.com
Quite straightforward: publishing port 80 lets me correctly access all those sites. Next, I have two services that I need to run, so I built two containers
service-a -> 172.17.0.3:3000
service-b -> 172.17.0.4:5000
and all is good, I can privately access those services from my docker host. The trouble comes when I want to publicly restrict access to service-a through service-a.bar.com:80 only, and to service-b through www.foo.com:5000 only. A lot of reading after, it would seem that I have to create a dreadful artefact called a proxy, or reverse-proxy, to make things more confusing. I have no idea what I'm doing, so I dove nose-first into nginx -- which I had never used before -- because someone told me it's better than Apache at dealing with lots of small tasks and requests -- not that I would know how to turn Apache into a proxy, mind you. Anyway, nginx sounded perfect for a thing that has to take a request a pass it onto another server, so I started reading docs and I produced the following (in addition to the correctly working vhosts):
upstream service-a-bar-com-80 {
server 172.17.0.3:3000;
}
server {
server_name service-a.bar.com;
listen 80;
location / {
proxy_pass http://service-a-bar-com-80;
proxy_redirect off;
}
}
upstream www-foo-com-5000 {
server 172.17.0.4:5000;
}
server {
server_name www.foo.com;
listen 5000;
location / {
proxy_pass http://www-foo-com-5000;
proxy_redirect off;
}
}
Which somewhat works, until I access http://blog.bar.com:5000 which brings up service-b. So, my question is: what am I doing wrong?
nginx (like Apache) always has a default server for a given ip+port combination. You only have one server listening on port 5000, so it is your defacto default server for services on port 5000.
So blog.bar.com (which I presume resolves to the same IP address as www.foo.com) will use the default server for port 5000.
If you want to prevent that server block being the default server for port 5000, set up another server block using the same port, and mark it with the default_server keyword, as follows:
server {
listen 5000 default_server;
root /var/empty;
}
You can use a number of techniques to render the server inaccessible.
See this document for more.

How do I serve multiple domains on the same port with nginx

For development, I'd like to serve multiple projects on different local domains, all on port 80. In my hosts file I direct local.example.com to localhost, same for local.example2.com.
Now I'm trying to convince nginx to serve the example resources for the one url, and the example2 resources for the other.
I've read the nginx documentation and this blog post. But I think I must be missing something.
I've added to my nginx.conf:
include /Users/iwein/Sites/conf/*.conf;
Then in sites I add configuration like example.conf:
server {
listen 80;
server_name local.example.com;
…
and example2.conf:
server {
listen 80;
server_name local.example2.com;
…
Now the weird thing is that nginx seems to load the alphabetically first config, but on the second url, it serves the resources from the first server definition too. Nginx seems to totally ignore the server_name. How should I configure for this use case?
UPDATE:
It appears that if you use only one separator in the domain name (e.g. example1.local), it works just fine. I didn't further pursue this, because I have better things to do, but it's odd.
Apparently, nginx doesn't like the format of my server names. If I remove the 'local' subdomain it seems to work much better. I'm now working with example.dev and example2.dev and the problem is gone.

Resources