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

Nginx Wildcard for try_files

· 52CE 我爱测

So I'm trying to setup a default nginx config file that can be deployed across numerous docker image with different website configs in each container. It is currently set as below: server { listen 80 default_server; root /var/www…

问题原文

原帖链接 · SyphonxZA · CC BY-SA 4.0

So I'm trying to setup a default nginx config file that can be deployed across numerous docker image with different website configs in each container. It is currently set as below:

server {
   listen 80 default_server;

   root /var/www/;
   index index.php index.html;

   server_name localhost;
   include php5-fpm.conf;


   location / {
           try_files $uri $uri/ /index.php?q=$request_uri;
   }

   location /status {
           stub_status on;
           access_log   off;
           allow 127.0.0.1;
           deny all;
   }

}

The problem is that it doesn't work for static sites that use index.htm or index.html

I can edit the location section like below and then the site will work. Is there any way to get nginx to use a wildcard, so index.* and it will use whichever index file it finds? index.* by itself does not work so not sure what needs to be added/removed...

       location / {
           try_files $uri /index.htm?q=$request_uri;
   }

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 4.0

You're fixated on the wrong part of try_files.

The part relevant to what you want is $uri/, with the trailing slash, which you seem to have removed. This bit causes nginx to try all of the files listed in the index directive.

You should put this part back in try_files and then edit the index directive to include the filenames you want.

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

资料来源与适用范围

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

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

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