工作中用到使用nginx配置一个端口下多个路径转发到html下不同目录,做一个总结记录。
目前知道的有三种方法可以实现,分别是使用root+location、alias、try_files。
假设一个场景,目前html目录下放了两个项目,目录名分别为test,demo,现在需要通过访问localhost/test访问test项目,通过访问localhost/demo访问demo项目。
1. root+location实现
使用root+location方法实现,只需要将要访问的目录作为location,root设置为项目所在的目录即可,当前场景下,即为配置一个location为/test/,然后将root设置为html,在配置一个location为/demo/,然后root设置为html,root配置文件路径的效果即为将请求转发到root路径/location/对应的路径下,root配置时,路径可以以/结尾,也可以不写,都不影响。具体配置如下:
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_re