Automatic trailing slash redirect and scheme

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Automatic trailing slash redirect and scheme

Jean-Daniel FISCHER
Hi,

I an trying to set the sheme used in automatic redirect generates by nginx when trailing slash is missing. The nginx server is behind a proxy that handles ssl, hence all requests are made using http so nginx use http in absolute redirect.

Is there a way to configure nginx to use the value of "$http_x_forwarded_proto" ?

The server conf:
server {
listen 8080;
server_name _;
gzip on;
gzip_disable "msie6";
root /usr/share/nginx/www;
# Prevent redirect to have port 8080
port_in_redirect off;

# 404
error_page 404 /404.html;

# Redir auto to http
if ($http_x_forwarded_proto = http) {
return 301 https://$host$request_uri;
}

# Ensure remote ip is the right one
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
real_ip_recursive on;

# Cache control on image
location ~ ^/fr/(.*\.(bmp|gif|jpeg|jpg|jxr|hdp|wdp|png|svg|svgz|tif|tiff|wbmp|webp|jng|cur|ico|woff|woff2))$ {
add_header Cache-Control public,max-age=86400;
alias /usr/share/nginx/www/$1;
}

# Serving data configuration
location ~ ^/fr/(.*) {
include /etc/nginx/redirect/*;
alias /usr/share/nginx/www/$1;
}
}
Regards,

--

Jean-Daniel Fischer
Developer  

+33 (0)2 40 18 04 77
16 Bd Charles de Gaulle, Bât. B
44800 Saint-Herblain, France 

LinkedInFacebookYouTubeInstagram


Cedreo est noté
 
 Trustpilot Stars  sur Trustpilot Logo 


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Automatic trailing slash redirect and scheme

Igor A. Ippolitov
Hello,

You can try adding an 'error_page 301 @returnme' and then a location like this:
location @returnme {
}

Regards,
Igor

On 22.08.2019 16:22, Jean-Daniel FISCHER wrote:
Hi,

I an trying to set the sheme used in automatic redirect generates by nginx when trailing slash is missing. The nginx server is behind a proxy that handles ssl, hence all requests are made using http so nginx use http in absolute redirect.

Is there a way to configure nginx to use the value of "$http_x_forwarded_proto" ?

The server conf:
server {
    listen  8080;
    server_name _;
    gzip on;
    gzip_disable "msie6";
    root /usr/share/nginx/www;
    # Prevent redirect to have port 8080
    port_in_redirect off;

    # 404
    error_page 404 /404.html;

    # Redir auto to http
    if ($http_x_forwarded_proto = http) {
        return 301 https://$host$request_uri;
    }

    # Ensure remote ip is the right one
    set_real_ip_from  0.0.0.0/0;
    real_ip_header    X-Forwarded-For;
    real_ip_recursive on;

    # Cache control on image
    location ~ ^/fr/(.*\.(bmp|gif|jpeg|jpg|jxr|hdp|wdp|png|svg|svgz|tif|tiff|wbmp|webp|jng|cur|ico|woff|woff2))$ {
        add_header Cache-Control public,max-age=86400;
        alias /usr/share/nginx/www/$1;
    }

    # Serving data configuration
    location ~ ^/fr/(.*) {
        include /etc/nginx/redirect/*;
        alias /usr/share/nginx/www/$1;
    }
}
Regards,

--


Jean-Daniel Fischer
Developer  

+33 (0)2 40 18 04 77
16 Bd Charles de Gaulle, Bât. B
44800 Saint-Herblain, France 

LinkedIn Facebook YouTube Instagram


Cedreo est noté
 
 Trustpilot Stars  sur Trustpilot Logo 


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx



_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Automatic trailing slash redirect and scheme

Francis Daly
In reply to this post by Jean-Daniel FISCHER
On Thu, Aug 22, 2019 at 03:22:38PM +0200, Jean-Daniel FISCHER wrote:

Hi there,

> I an trying to set the sheme used in automatic redirect generates by nginx
> when trailing slash is missing. The nginx server is behind a proxy that
> handles ssl, hence all requests are made using http so nginx use http in
> absolute redirect.
>
> Is there a way to configure nginx to use the value of
> "$http_x_forwarded_proto" ?

I think "not directly".

So, if the ssl-handling proxy does not have the equivalent of
proxy_redirect (http://nginx.org/r/proxy_redirect) to modify the Location:
header before it goes to the client; then you could use "absolute_redirect
off" (http://nginx.org/r/absolute_redirect) so that nginx will omit the
scheme and host and port from the Location: header, which all current
clients should Just Work with.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Automatic trailing slash redirect and scheme

Jean-Daniel FISCHER
Thanks for all the reply, I activate  "absolute_redirect off". 

Le ven. 23 août 2019 à 00:07, Francis Daly <[hidden email]> a écrit :
On Thu, Aug 22, 2019 at 03:22:38PM +0200, Jean-Daniel FISCHER wrote:

Hi there,

> I an trying to set the sheme used in automatic redirect generates by nginx
> when trailing slash is missing. The nginx server is behind a proxy that
> handles ssl, hence all requests are made using http so nginx use http in
> absolute redirect.
>
> Is there a way to configure nginx to use the value of
> "$http_x_forwarded_proto" ?

I think "not directly".

So, if the ssl-handling proxy does not have the equivalent of
proxy_redirect (http://nginx.org/r/proxy_redirect) to modify the Location:
header before it goes to the client; then you could use "absolute_redirect
off" (http://nginx.org/r/absolute_redirect) so that nginx will omit the
scheme and host and port from the Location: header, which all current
clients should Just Work with.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx


--

Jean-Daniel Fischer
Developer  

+33 (0)2 40 18 04 77
16 Bd Charles de Gaulle, Bât. B
44800 Saint-Herblain, France 

LinkedInFacebookYouTubeInstagram


Cedreo est noté
 
 Trustpilot Stars  sur Trustpilot Logo 


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx