nginx reverse proxy between 2 https servers - nginx

I'm a bit new to using nginx so I'm likely missing something obvious. I'm trying to create an nginx server that will reverse proxy to a set of web servers that use https.
I've been able to get it to work with one server list this:
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://<server1>.herokuapp.com;
}
}
However, as soon I try to add in the 'upstream' configuration element it no longer works.
upstream backend {
server <server1>.herokuapp.com;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
I've tried adding in 443, but that also fails.
upstream backend {
server <server1>.herokuapp.com:443;
}
server {
listen $PORT;
server_name <nginx server>.herokuapp.com;
location / {
proxy_pass https://backend;
}
}
Any ideas what I'm doing wrong here?

Related

Why Nginx HTTP forward proxy does not work?

I have 2 ways of doing a forward proxy in nginx.
My problem is that only one of them works. The second one does not work.
This one works
stream {
server {
listen 8200;
proxy_pass 127.0.0.1:8000;
}
}
This one does not work
http{
server {
listen 8200;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
}
My question is why the second method does not work for me?

nginx load balancer configuration not working

I have a very simple load balancing configuration, set it up for PoC purpose. My app server1 and load balancer server is same.Below is my load balncer conf file content. Please help me is this correct?
At the moment, whenever all my request goes to IP1. I expect it to route traffic to IP2 as well whenever I hit IP1, please correct if this understanding is wrong.
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
Your configuration is correct. Sending multiple requests to your NGINX Proxy Port 80 will Loadbalance the traffic with the default LB-Algorithem round-robin to one of your backend (upstream) servers.
Check this out:
https://www.nginx.com/resources/wiki/start/topics/examples/loadbalanceexample/
http {
upstream myproject {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name www.domain.com;
location / {
proxy_pass http://myproject;
}
}
}
You can try this from any Linux command line
for ((i=1;i<=10;i++)); do curl -v "http://localhost"; sleep 1; done
This should print AppServer1, AppServer2, AppServer3 and start again from 1.
A demo-backend could look like
server {
listen 8080;
location / {
return 200 "AppServer1\n";
}
}
server {
listen 8081;
location / {
return 200 "AppServer2\n";
}
}
server {
listen 8082;
location / {
return 200 "AppServer3\n";
}
}
I have just tested in a fresh nginx docker container without any problem.

Two reverse proxy on one server, nginx

Is it possible?
I want know how would be work this config.
Is it Ok or not and why?
upstream one_proxy {
ip_hash;
server unix:/var/run/websocket-proxy.20000.sock max_fails=0;
server unix:/var/run/websocket-proxy.20001.sock max_fails=0;
}
upstream two_proxy {
ip_hash;
server 1.2.3.4:1234;
}
server {
server_name domain_name;
listen 0.0.0.0:80;
access_log off;
location / {
proxy_pass http://one_proxy;
}
}
server {
listen 127.0.0.1:20003;
access_log off;
location / {
proxy_pass http://two_proxy;
}
}
Definitely config not complete but I think it look like good.
I didn't find examples with two reverse proxy on one nginx and I doubt.
If you have experience share it please )
Don't you know nginx -t -c conf/your-custom-nginx.conf command could test the configuration

Nginx load balancing does not forward correctly

Nginx forwards call in a wrong way when configured with upstream!
Not working
upstream search {
server some.server.com;
}
server {
listen 80;
location / {
proxy_pass http://search;
}
}
Working good
upstream search {
server some.server.com;
}
server {
listen 80;
location / {
proxy_pass http://some.server.com;
}
}
When configured with upstream - target server returns "404 - Resource not found"
What do I do wrong?
Problem was in 'HOST' header
in case of upstream header 'HOST' is set to search.
To fix that you need to replace 'HOST' header in request
location / {
proxy_set_header Host some.server.com;
proxy_pass http://search;
}

nginx reverse proxy using upstream directive

I want to configure nginx to be a reverse proxy using upstream directive (and add there keepalive for example).
upstream my_backend {
server 127.0.0.1:3579;
}
server {
listen 80;
location / {
proxy_pass http://my_backend;
}
}
But the problem is that it returns Bad Request (Invalid host). And there is nothing in nginx error log to help me solve it.
Everything else being the same this configuration without upstream directive works as expected:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:3579;
}
}
Aren't those two equivalent? And what do I have to do to make it work with upstream?

Resources