I'm building nginx for reverse_proxy with Spring and Docker
Here is my configuration.
nginx.conf
user proxy;
worker_processes auto;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include mime.types;
default_type application/octet-stream;
include /home/wertkey/nginx/conf/conf.d/*.conf;
client_max_body_size 200M;
client_header_buffer_size 48k;
server {
listen 7070 default_server;
listen [::]:7070 default_server;
server_name _;
root /usr/share/nginx/html;
location / {
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
conf.d/default.conf
server {
listen 7070;
server_name something.com;
proxy_connect_timeout 900s;
proxy_send_timeout 900s;
proxy_read_timeout 900s;
send_timeout 900s;
location ^~ /editor/ {
proxy_pass http://localhost:8081;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
rewrite ^/editor/(/.*)$ $1 break;
}
}
docker
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6a6a98a25a9 editor "/bin/sh -c /usr/l..." 8 minutes ago Up 8 minutes 0.0.0.0:8081->8080/tcp editor
My spring server is running at 7070 port. I want to be this way.
- request something.com:7070/api
- nginx proxy_pass to 8081
- and docker 8081 -> 8080
But response always return 502 Bad Gateway
What am i missing?
+ I am currently running two nginxs on the same server. One is running at 8080 port and the other is running at 7070 port. 8080 nginx is for web servers and 7070 nginx is for proxy purposes. The log does not appear in 7070 nginx and the log appears in 8080 nginx. I don't know why the request goes to 8080 nginx. How can I send the request to 7070 nginx?
When a process inside docker tries to connect to
localhost:8081
that translates to "port 8081 on the docker container", and NOT "port 8081 on the host".For your setup, I'd encourage you to try docker-compose. With docker-compose, each container is accessible for other containers by its name (e.g.: if your containers are named
backend
andnginx
, the latter can access the former viabackend:8081
).This is especially handy if you're using these during development, since docker-compose let you quickly start all the containers and manage them through a single interface.
Another alternative is to change the network type on the containers.
The issue could be related to the rewrite directive in the location block. What that's doing is essentially a redirect on the client side. A possible solution would be to use the regex in the location itself rather than the rewrite: