我有成千上万的文本文件被命名为ABC 。 我想从不同的URL访问这些文件: – 如果它是ABC或ABC.txt我想添加default_type text/plain并提供该文件 – 如果它是ABC.html我想添加default_type text/html并提供该文件(以便我有相同的文件的两个不同版本)。
是否有可能在nginx中做到这一点? 一个url只能与一个location指令匹配的事实,导致我find一个“干净”的方式麻烦。
您应该尝试为每个文件扩展名创build一个单独的位置,并使用内部redirect。 这将是这样的事情:
location /ABC.txt { add_header Content-Type text/plain; rewrite /ABC last; } location /ABC.html { add_header Content-Type text/html; rewrite /ABC last; } location /ABC { alias /var/www/ABC.txt }
我不承诺它会工作,因为我没有在真正的nginx上testing它。
nginx根据URI处理build议的mimetype。 我试了下面的组合,它的工作原理:
root /usr/share/nginx/www; default_type text/plain; location = /abc.txt { alias /usr/share/nginx/www/abc; } location /abc.html { alias /usr/share/nginx/www/abc; }