大网数据广告活动
服务器运维问答

Configuring Nginx reverse proxy to add index.html on incoming http requests when only path name exists

· 52CE 我爱测

I need to set up nginx as a reverse proxy for my static site hosted on DO spaces (S3 type object storage). The problem with their s3 is that if i request a path name without any specific file name then it does not give index.html…

问题原文

原帖链接 · Muhammad Ebrahym · CC BY-SA 4.0

I need to set up nginx as a reverse proxy for my static site hosted on DO spaces (S3 type object storage).

The problem with their s3 is that if i request a path name without any specific file name then it does not give index.html as the default (which we were expecting)

if i request s3.example.com/some/path/ then it gives an error instead of serving index.html in that folder. DO knows about this issue but is not going to resolve it any time soon.

We are stuck because we have spent a lot of time and effort migrating to DO. All our paths are in this format because we are migrating from some other setup. we want s3.example.com/some/path/ to load s3.example.com/some/path/index.html - which is normal web server behavior.

Can nginx help as a reverse proxy? In any case we would be using a reverse proxy in front of the subdomain and doing a proxy_pass ie example.com/some/path/ would be proxy_pass to s3.example.com/some/path/ - this is using normal proxy_pass

I wanted to know if we could add the index.html while doing the proxy_pass - BUT only for requests that do not have any file name. For those requests that have some file name associated with it like example.com/som/path/image.jpg or some other html or other file then we would not want to add the index.html of course.

The issue is that many a times there is no ending / in the url the url is like 1 example.com/some/path OR like 2 example.com/some/path/

And in some other cases it will be 3 example.com/some/path/ex.html (or jpg or whatever)

In the first case we want to add index.html In second case /index.html In 3rd case nothing

How would the config look like in this situation?

Thank you in advance...

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

You could rewrite every URI that either ends with / or does not contain a . in the last path element, before passing it to the upstream server.

For example:

server {
    ...
    location / {
        ...
        rewrite ^(.*)/$ $1/index.html break;
        rewrite ^(.*/[^./]+)$ $1/index.html break;
        proxy_pass ...;
    }
}

See this document for details.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-07-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。