I'm serving my app with uWSGI using uwsgi --http-socket 127.0.0.1:3031 -w app:app, which works when I go to 127.0.0.1:3031 in a browser. I want to use Nginx, so I told it to uwsgi_pass to that url, but now I get a 502 Bad Gateway error. How do I put uWSGI behind Nginx?
server {
listen 8080;
server_name 127.0.0.1;
location / {
uwsgi_pass 127.0.0.1:3031;
include uwsgi_params;
}
location /static {
alias /static/folder/location;
}
}
2016/05/16 19:50:09 [error] 6810#0: *4 upstream prematurely closed
connection while reading response header from upstream, client:
127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", upstream:
"uwsgi://127.0.0.1:3031", host: "127.0.0.1:8080"
You can use http-socket between nginx and uWSGI.
For example, if you launch your python app with uWSGI:
uwsgi --http-socket 127.0.0.1:3031 --wsgi-file application.py --callable app --processes 4 --threads 2 --stats 127.0.0.1:9191
Configure Nginx with:
location / {
proxy_pass http://127.0.0.1:3031/;
}
Use socket, not http-socket.
uwsgi --socket 127.0.0.1:3031 -w app:app
http-socket makes uWSGI act like a web server that speaks HTTP, and is not correct if you're using Nginx, since it understands uWSGI directly.
Related
I have the following nginx configuration which returns 502
server {
listen 2052;
listen 2082;
server_name good.abc.com;
location / {
proxy_pass http://goodServer:$server_port;
}
}
Testing:
proxy_pass http://goodServer:2052; When the fixed port is 2052
curl good.abc.com:2052 It can be accessed normally.
Question:
The port I want to forward user requests to
For example.
curl good.abc.com:2052 ---> goodServer:2052
curl good.abc.com:2082 ---> goodServer:2082
So the port must be a variable, just like $server_port
Log:
2021/04/04 14:10:11 [error] 24#24: *19 good could not be resolved (3: Host not found), client: 162.158.91.119, server: good.abc.com, request: "GET / HTTP/1.1", host: "good.abc.com:2052"
I have two domains, let's say ex.io and example.com. I own both and have access to both servers. Both run NGINX.
Goal: I'd like to have any request from ex.io forward to a specific path under example.com, e.g. http://ex.io passes to https://example.com/foo/bar. This has to be done without a redirect (more in Context)
Context: The goal is to host a shell script at https://example.com/foo/bar so that curl ex.io | sh will run the shell script. I'd like no redirects to happen so no additional flags are needed for curl.
My current .conf setup for both servers follows:
server {
listen 80;
listen [::]:80;
server_name example.com
return 308 https://$host$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name ex.io;
return 308 https://example.com/foo/bar;
}
# ...
This works, but requires the extra flag -L to run properly under curl. How can I proxy ex.io to go directly to https://example.io/foo/bar?
EDIT: I'm forwarding http://ex.io/ to https://example.com/foo/bar which may be tricky going from http to https. Bananenkönig's response fails with a 502 Bad Gateway error and the following logs:
2020/10/26 23:28:45 [error] 223#223: *281 SSL_do_handshake() failed (SSL: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:SSL alert number 40) while SSL handshaking to upstream, client: XXX.XXX.XXX.XXX, server: ex.io, request: "GET / HTTP/1.1", upstream: "https://XXX.XXX.XXX.XXX:443/foo/bar/", host: "ex.io"
2020/10/26 23:28:45 [warn] 223#223: *281 upstream server temporarily disabled while SSL handshaking to upstream, client: XXX.XXX.XXX.XXX, server: ex.io, request: "GET / HTTP/1.1", upstream: "https://XXX.XXX.XXX.XXX:443/foo/bar/", host: "ex.io"
I would try it like this:
server {
listen 80;
listen [::]:80;
server_name ex.io;
location /some/location/on/ex.io/ {
proxy_pass https://example.com/some/location/on/example.com/;
}
}
when you want ex.io/ (on location /) to be forwarded to example.com/... write "location / { "
I've a service listening to 8080 port. This one is not a container.
Then, I've created a nginx container using official image:
docker run --name nginx -d -v /root/nginx/conf:/etc/nginx/conf.d -p 443:443 -p 80:80 nginx
After all:
# netstat -tupln | grep 443
tcp6 0 0 :::443 :::* LISTEN 3482/docker-proxy
# netstat -tupln | grep 80
tcp6 0 0 :::80 :::* LISTEN 3489/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 1009/java
Nginx configuration is:
upstream eighty {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name eighty.domain.com;
location / {
proxy_pass http://eighty;
}
}
I've checked I'm able to connect with with this server with # curl http://127.0.0.1:8080
<html><head><meta http-equiv='refresh'
content='1;url=/login?from=%2F'/><script>window.location.replace('/login?from=%2F');</script></head><body
style='background-color:white; color:white;'>
...
It seems running well, however, when I'm trying to access using my browser, nginx tells bt a 502 bad gateway response.
I'm figuring out it can be a problem related with the visibility between a open by a non-containerized process and a container. Can I container stablish connection to a port open by other non-container process?
EDIT
Logs where upstream { server 127.0.0.1:8080; }:
2016/07/13 09:06:53 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 62.57.217.25, server: eighty.domain.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "eighty.domain.com"
62.57.217.25 - - [13/Jul/2016:09:06:53 +0000] "GET / HTTP/1.1" 502 173 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-"
Logs where upstream { server 0.0.0.0:8080; }:
62.57.217.25 - - [13/Jul/2016:09:00:30 +0000] "GET / HTTP/1.1" 502 173 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-" 2016/07/13 09:00:30 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client:
62.57.217.25, server: eighty.domain.com, request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8080/", host: "eighty.domain.com" 2016/07/13 09:00:32 [error] 5#5: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 62.57.217.25, server: eighty.domain.com, request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8080/", host: "eighty.domain.com"
62.57.217.25 - - [13/Jul/2016:09:00:32 +0000] "GET / HTTP/1.1" 502 173 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" "-"
Any ideas?
The Problem
Localhost is a bit tricky when it comes to containers. Within a docker container, localhost points to the container itself.
This means, with an upstream like this:
upstream foo{
server 127.0.0.1:8080;
}
or
upstream foo{
server 0.0.0.0:8080;
}
you are telling nginx to pass your request to the local host.
But in the context of a docker-container, localhost (and the corresponding ip addresses) are pointing to the container itself:
by addressing 127.0.0.1 you will never reach your host machine, if your container is not on the host network.
Solutions
Host Networking
You can choose to run nginx on the same network as your host:
docker run --name nginx -d -v /root/nginx/conf:/etc/nginx/conf.d --net=host nginx
Note that you do not need to expose any ports in this case.
This works though you lose the benefit of docker networking. If you have multiple containers that should communicate through the docker network, this approach can be a problem. If you just want to deploy nginx with docker and do not want to use any advanced docker network features, this approach is fine.
Access the hosts remote IP Address
Another approach is to reconfigure your nginx upstream directive to directly connect to your host machine by adding its remote IP address:
upstream foo{
//insert your hosts ip here
server 192.168.99.100:8080;
}
The container will now go through the network stack and resolve your host correctly:
You can also use your DNS name if you have one. Make sure docker knows about your DNS server.
For me helped this line of code proxy_set_header Host $http_host;
server {
listen 80;
server_name localhost;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://myserver;
}
Just to complete other answers, I'm using mac for development and using host.docker.internal directly on upstream worked for me and no need to pass the host remote IP address. Here is config of the proxy nginx:
events { worker_connections 1024; }
http {
upstream app1 {
server host.docker.internal:81;
}
upstream app1 {
server host.docker.internal:82;
}
server {
listen 80;
server_name app1.com;
location / {
proxy_pass http://app1;
}
}
server {
listen 80;
server_name app2.com;
location / {
proxy_pass http://app2;
}
}
}
As you can see, I used different ports for different apps behind the nginx proxy. I used port 81 for the app1 and port 82 for the app2 and both app1 and app2 have their own nginx containers:
For app1:
docker run --name nginx -d -p 81:80 nginx
For app2:
docker run --name nginx -d -p 82:80 nginx
Also, please refer to this link for more details:
docker doc for mac
What you can do is configure proxy_pass that from container perspective the adress will be pointing to your real host.
To get host address from container perspective you can do as following on Windows with docker 18.03 (or more recent):
Run bash on container from host where image name is nginx (works on Alpine Linux distribution):
docker run -it nginx /bin/ash
Then run inside container
/ # nslookup host.docker.internal
Name: host.docker.internal
Address 1: 192.168.65.2
192.168.65.2 is the host's IP - not the bridge IP like in spinus accepted answer.
I am using here host.docker.internal:
The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Windows.
Then you can change nginx config to:
proxy_pass http://192.168.65.2:{your_app_port};
and it should work fine.
Remember to provide the same port as your local application runs with.
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
complete reference: https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
nginx.sh
ip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -n 1)
docker run --name nginx --add-host="host:${ip}" -p 80:80 -d nginx
nginx.conf
location / {
...
proxy_pass http://host:8080/;
}
It‘s works for me
I had this issue and it turned out to be an issue with the docker container not starting up due to a permissions issue.
In my case running
docker-compose ps
showed that the container had not started and exited with status 1. Turns out the permissions had been lost in migrating to a new machine. Adjusting the permissions to a know staff user on the parent directory fixed the problem for me and I was then able to start docker service where as previously I was getting
nginx_1_c18a7f6f7d6d | chown: /var/www/html: Operation not permitted
I'm experiencing 502 gateway errors when accessing my IP on nginx(http://52.xx.xx.xx/), the logs simply says this:
2015/09/18 13:03:37 [error] 32636#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: xx.xx.xx.xx, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "xx.xx.xx.xx"
my nginx.conf file
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name xx.xx.xx.xx; # substitute your machine's IP address or FQDN
charset utf-8;
access_log /home/ubuntu/test_django/nginx_access.log;
error_log /home/ubuntu/test_django/nginx_error.log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/test_django/static/media/; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/test_django/static/; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/test_django/uwsgi_params; # the uwsgi_params file you installed
}
}
Is there anything wrong with nginx.conf file.....if i use default conf then it is working.
I resolved it by changing the socket configuration in uwsgi.ini
from socket = 127.0.0.1:3031, to socket = :3031. I was facing this issue when I ran nginx in one Docker container and uWSGI in another. If you are using command line to start uWSGI then do uwsgi --socket :3031.
Hope this helps someone stuck with the same issue, during deployment of a Django application using Docker.
change this address:
include /home/ubuntu/test_django/uwsgi_params;
to
include /etc/nginx/uwsgi_params;
I ran into this issue when setting up the env by nginx + gunicorn and solve it by
adding '*' to ALLOWED_HOSTS or your specific domain.
In my case with a debian server it worked moving:
include /etc/nginx/uwsgi_params;
In the location tag in my nginx server config file, like this:
location /sistema {
include /etc/nginx/uwsgi_params;
uwsgi_pass unix://path/sistema.sock;
}
Also, check you have the following packages installed:
uwsgi-plugin-python
pip3 install uWSGI did the trick for me :D
So. I am using Nginx as a load balancer to load traffic between couple of instances.
Let's say my Nginx loadbalancer is at platform.staging.com (example).
I am trying to redirect traffic from
platform.staging.com/sync
To one of these:
sync1.staging.com:12345
sync2.staging.com:12345
Notice that what I am trying to achieve is to have /sync part stripped down and requests to sync instances should have path /.
This is what I tried but it doesn't work:
upstream sync-cluster {
ip_hash;
server sync1.staging.com:12345;
server sync2.staging.com:12345;
}
server {
listen 443 ssl spdy;
server_name platform.staging.com;
location /sync {
proxy_pass http://sync-cluster;
}
}
In the logs I can see:
2014/01/14 23:20:38 [error] 2385#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: XX.XXX.XX.XXX, server: platform.staging.com, request: "GET /sync HTTP/1.1", upstream: "http://X.X.X.XXX:12345/sync", host: "platform.staging.com"
Try adding a rewrite before doing the proxy pass, I'll assume you are going to preserve what's after /sync, hope this works for you
location ^~ /sync(.*) {
rewrite ^ $1;
proxy_pass ...;
}