2

Recently, I had issues with nginx placing the port number after redirects if the URL was missing the trailing slash (for example: https://example.com/thing would redirect to https://example:8080/thing/). So, I added the line for port_in_redirect off; and that fixed the issue. However, it's now causing another issue. If the url is missing the trailing slash, it redirects to HTTP.

https://example.com/thing will redirect to http://example/thing/, which causes a failed request.

This is what my nginx.conf looks like:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  /var/log/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format logstash_json '{ "@timestamp": "$time_iso8601", '
                          '"@fields": { '
                          '"remote_addr": "$remote_addr", '
                          '"remote_user": "$remote_user", '
                          '"request": "$request", '
                          '"status": "$status", '
                          '"body_bytes_sent": "$body_bytes_sent", '
                          '"request_time": "$request_time", '
                          '"request_method": "$request_method", '
                          '"http_referrer": "$http_referer", '
                          '"http_user_agent": "$http_user_agent" } }';

    access_log  /var/log/access.log  logstash_json;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;            # Port to listen on
        server_name  localhost;       # Servername
        client_max_body_size 0;       # Max upload size
        chunked_transfer_encoding on; # Support for chunked transfer (upload)
        port_in_redirect off;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    #include servers/*;
}
  • see: proxy_redirect – Geoffrey Apr 2 '18 at 18:17
  • There is no https configuration here. Did you terminate TLS somewhere else? – Michael Hampton Apr 2 '18 at 18:55
  • Not that I know of. HTTPS works with this current config--it's only redirects that aren't working. – Nxt3 Apr 2 '18 at 18:57
  • OK, then your configuration is incomplete. Please show the complete configuration. – Michael Hampton Apr 4 '18 at 21:09
5

By default, nginx issues an absolute URL in the 3xx response, which includes the scheme used to connect to the server. Your server at port 8080 is connected to over http, so that is the scheme that appears in the 3xx response.

Since version 1.11.8, nginx can be configured to issue a relative URL instead, which removes the scheme and hostname from the URL.

absolute_redirect off;

See this document for details.


If you are using an older version of nginx (and upgrading it is not an option) you might be able to override the default behaviour by using an explicit if...return statement.

Your existing configuration seems fairly simple:

location / {
    root   html;
    index  index.html index.htm;
}

There are a number of edge cases, so the solution can become quite complex, but something like this might work for you:

root html;

location ~ /$ {
    try_files "${uri}index.html" "${uri}index.htm" =404;
}
location / {
    try_files $uri @rewrite;
}
location @rewrite {
    if (-d $request_filename) { 
        return https://$host$uri/$is_args$args; 
    }
}
| improve this answer | |
  • Putting this in my server block causes 502 errors when trying to access anything now. I'm going to try placing it in the HTTP block of the config, but I don't expect that to fix it either. – Nxt3 Apr 3 '18 at 13:59
  • 1
    502 errors should be reported in the error log – Richard Smith Apr 3 '18 at 15:37
  • I guess what I meant was that when I then try to navigate to a page, the page is basic HTML that says "502 BAD GATEWAY". – Nxt3 Apr 3 '18 at 16:13
  • 1
    I understand. But the reason for the bad gateway error should be expanded in the error log of one of your servers. If the proxy is reporting bad gateway, it may be because 8080 failed to restart, in which case you should test the configuration using nginx -t. But its just a guess - you are the one with access to the error logs ;-) – Richard Smith Apr 3 '18 at 16:20
  • Okay so I figured out the issue. We're not using nginx 1.11.8. What is the fix if I'm not able to use absolute_redirect off;? – Nxt3 Apr 4 '18 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.