nginx:“ /” URL不会尝试使用index.html文件

时间:2018-09-15 21:22:13

标签: nginx

在这种配置下,当我手动调用“ my.domainname.com/unity/index.html”时,它会工作并且会查找所有其他文件。 当我尝试使用“ my.domainname.com/unity/”时,它不会转到默认的index.html文件并返回403。我缺少什么?

(最重要的是最新的行,用于配置用户何时要访问/unity路径)

server {
  listen *:443 ssl;
  server_name "~^my\.domainname\..*$";

  index index.html index.htm;

  access_log /var/log/nginx/proxy-access.log proxylog;
  error_log /var/log/nginx/proxy-error.log error;

  ssl_certificate /var/lib/acme/live/my.domainname.com/fullchain;
  ssl_certificate_key /var/lib/acme/live/my.domainname.com/privkey;
  ssl_trusted_certificate /var/lib/acme/live/hive.battlesoop.fr/chain;
  ssl_stapling on;
  ssl_stapling_verify on;

  location '/.well-known/acme-challenge' {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Server $host;
    proxy_pass http://acmetool;
  }

  # Add CORS access to all = '*'
  add_header 'Access-Control-Allow-Origin' '*' always;
  add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always;
  add_header 'Access-Control-Allow-Credentials' 'true' always;

  # unity
  location ~* ^/unity(?<p>.*) {
    root /web/htdocs/my.domainname.com/unity;
    index index.html index.htm;
    try_files /$p =403;
    access_log off;
    expires 1h;
  }
}

1 个答案:

答案 0 :(得分:1)

对于try_filesindex功能由带有后缀/ file 术语触发。有关详细信息,请参见this document

虽然我无法使其与您的命名捕获一起使用,但是如果您在root语句的末尾取消“ / unity”一词,则可以使用常规方法。

例如:

location ~* ^/unity {
    root /web/htdocs/my.domainname.com;
    index index.html index.htm;
    try_files $uri $uri/ =403;
    access_log off;
    expires 1h;
}