I'm using Docker to run nginx on macOS to serve static files on http://localhost:8080
.
When clicking on an anchor link, the port is being lost.
For example, clicking on <a href="/foo">
will link to http://localhost/foo
instead of http://localhost:8080/foo
.
My nginx config is:
server {
listen 80 default_server;
server_name _;
root /var/www/root;
index index.html index.php;
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
error_page 404 /404.html;
location / {
try_files $uri $uri/ $uri.html =404;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
The directory structure of /var/www/root
is:
├── foo
│ └── index.html
├── bar
│ └── index.html
└── baz
└── index.html
However, when changing:
try_files $uri $uri/ $uri.html =404;
to:
try_files $uri/index.html $uri $uri/ $uri.html =404;
the anchor links link correctly.
However, the following does not work:
try_files $uri/ $uri $uri.html =404;
What is going on exactly? Is there a way to exclude $uri/index.html
from try_files
and have the anchor links preserve the port?
Update
This works, but I'm not quite sure why.
if (-d $request_filename) {
rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}
location / {
try_files $uri $uri/ $uri.html =404;
}