当 URL 包含文件扩展名时,我可以在 nginx 中 return 404 吗?

Can I return 404 in nginx when the URL contains a file extension?

我是 nginx 新手,需要将应用程序的 nginx 配置文件修改为在 URL 包含文件扩展名时始终 return 404。我的nginx.conf文件如下:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen 80;
        server_name  localhost;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            try_files $uri $uri/ /index.html;
            expires 5m;
        }

        location /appsettings.json {
            expires 5m;
        }

        location /version.json {
            expires 5m;
        }
    }
}

如有任何建议,我们将不胜感激:)

将 ext 替换为您要禁止的扩展名:

location ~ \.ext$ {
  return 404;
}

如果你想禁止所有扩展(真正带点的任何东西):

location ~ \. {
  return 404;
}