#StackBounty: #docker #nginx #reverse-proxy #nginx-reverse-proxy #x-forwarded-for How to configure nginx X-Forwarded-Port to be the ori…

Bounty: 50

I am using nginx in a standard reverse proxy scenario, to pass all requests to /auth to another host, however I’m trying to use non-standard ports.

My end goal is to have the X-Forwarded-Port header set to the port that the request comes in on.

Here is my location block in nginx.conf:

location /auth/ {
    proxy_pass       http://otherhost:8090;
    proxy_set_header Host $host;
    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 X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port <VAR>;

This nginx is running in a docker container, that is configured to forward requests from 8085 into 80 in the container, such that the nginx process is listening on 80:>80/tcp

When I hit the URL:


I am correctly redirected to http://otherhost:8090, but the X-Forwarded-Port header is missing or wrong.

Where I have <VAR> in the original block, I have tried the following:

  • $server_port – This is the port nginx is listening on (80), not the
    request port.

  • $pass_port – Seems to be null in my setup, so nginx drops the

  • $http_port – This is a random port per request.

  • $remote_port – This is a random port per request.

I can change my config at deploy time to hardcode to the known port of incoming requests, but ideally I would be able to change the front port without any change to the nginx config.

I’ve scoured the nginx variable list but can’t find anything like $request_port. Is there any way for me to achieve my intent?

Get this bounty!!!

#StackBounty: #java #nginx #spring-cloud #nginx-reverse-proxy #spring-cloud-gateway Gateway timeout with Sping cloud gateway and Nginx …

Bounty: 100

I created API gateway for my application and it will act as a front controller for other microservices.
In my production setup I user Nginx as a reverse proxy for my gateway

API gateway is running on port 8080

Nginx config as below


server {
    listen 80;
    server_name api.example.com;
    location / {
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://localhost:30010/;
        keepalive_timeout 500s;
    keepalive_timeout 500s;
    access_log /var/log/nginx/api.log;  
    error_log /var/log/nginx/api_error.log;

timeout setting in nginx.conf

proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
send_timeout 300;

Spring cloud gateway gradle file:



Gateway application:

@ComponentScan(basePackages = {"com.example"})
@EntityScan(basePackages = {"com.example"})
@EnableFeignClients(basePackages = "com.example")
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);

Problem statement:

In one of my microservice, one REST API take more than 3 minutes to complete.
If I call this API via nginx(api.example.com), it fails exactly after 1 min and gives HTTP status 504


curl --request GET 
  --url http://api.example.com/hellomicroservice/api/take/moretime


504 Timeout while reading the response from Server

No error logs in nginx and API gateway

Access log from nginx: - - [01/Apr/2019:08:14:33 +0000] "GET hellomicroservice/api/take/moretime HTTP/1.1" 499 0 "-" "PostmanRuntime/7.3.0"

But when I make a call of the same API directly to the gateway(on gateway port 8080), request process successfully

curl with gateway port:

curl --request GET 
  --url http://api.example.com:8080/hellomicroservice/api/take/moretime

If I apply the Nginx timeout settings as less than 60 Seconds(For example 30 seconds), the request gets timed out in a specified time interval. But if I set the Nginx timeout to be more than 60 seconds, let’s 300 Seconds, the request gets timed out after 60 seconds.

Get this bounty!!!