mardi 11 septembre 2018

How to serve static files when Nginx used to proxy the same host?

For testing purpose I needed delayed responses from a NIGNX server serving static files as response. I've tried to use this description using nginx proxying but I would use the same host for the proxy_pass.

It was really hard to "find" (serve) the static files after the proxying. Now I have a working config but I am wondering if there any more simply or elegant way to realize this config.

The main question is how to provide test_1|test_2/index.xml without setting an "internal" alias on them.

The request is coming to example.com/test_1|test_2 and the response is a static xml file located in /var/www/test_1|test_2 after a delay.

server {
    listen            80;
    listen            [::]:80;
    server_name       example.com;

    location / {
        echo_sleep    0.1;  # delay
        echo_exec     /proxy$request_uri;
    }

    location /proxy/ {
        internal;
        proxy_pass          http://$host;
        proxy_set_header    Host                $http_host;
        proxy_set_header    X-Forwarded-For     $remote_addr;
    }

    location /proxy/test_1 {
        internal;
        alias /var/www/test_1/index.xml;
    }

    location /proxy/test_2 {
        internal;
        alias /var/www/test_2/index.xml;
    }

    error_page  405     =200    $uri;  # handle post requests for testing purpose
}

Aucun commentaire:

Enregistrer un commentaire