0

I'm using a nginx container that is unprivileged and runs on port 8080, I used it to serve up a Hugo site, everything works except for when you click on a tag which sends you to /tags/mytag/ I keep getting a 301 redirect and since nginx inside the container is running on port 8080 and my site is on 443 I get an error because nginx returns the site with the port i.e. https://mysite:8080/tags/mytag. Now I found this link nginx docs

The issue is how would I use that to fix my problem I've messed around and I haven't been able to find the right answer, I've done something like

location /tags/ {
  proxy_pass http://localhost:5000/tags;
}

and

location = /tags {
  proxy_pass http://localhost:5000/tags/;
}

Of course neither worked. I'm a bit out of my league here any help would be appreciated.

After digging I think I found a solution it works running the container in docker now I need to test if works in k8s as I have an ingress controller in front of everything:

server {
    listen       5000;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /tags {
      alias /usr/share/nginx/html/tags;
    if (-d $request_filename) {
        rewrite [^/]$ $scheme://$http_host$uri/ permanent;
       }
    }

    #error_page  404              /404.html;

    error_page   404             /404.html;
    location = /404.html {
        root  /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/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;
    #}
}

I found the answer here

1 Answers1

0

It is not nginx that is returning the site with the port. It is your application that is returning the redirect.

You need to fix application configuration so that its root URL is set correctly to https://example.com.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63