nginx作为代理服务器,可以配置多个location,通过访问不同路径来访问不同目录。
比如:location / 用于访问官网首页,location /docs 用于访问帮助文档
- server {
- listen 8088;
- server_name localhost;
-
- #charset koi8-r;
-
- #access_log logs/host.access.log main;
-
- location / {
- root /opt/auto_test/testReport; #root末尾可以不用/结束
- index index.html index.htm;
- }
-
- location /docs {
- alias /opt/auto_test/docs/; #alias末尾必须要用/结束
- index index.html index.htm;
- }
-
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
-
- }
配置完后重启nginx,然后可以通过http://IP:8088/访问首页,通过http://IP:8088/docs访问帮助文档(前提是/opt/auto_test/docs/目录下面有index.html页面)
root和alias都可以定义在location模块中,都是用来指定请求资源的真实路径,比如:
- location /abc/ {
- root /data/www;
- }
请求http://IP:port/abc/123.png时,那么在服务器里面对应的真正的资源是:/data/www/abc/123.png
注意:root真实路径是root指定的值加上location指定的值。
而 alias 正如其名,alias指定的路径是location的别名,不管location的值怎么写,资源的真实路径都是 alias 指定的路径,比如:
- location /abc/ {
- alias /data/www;
- }
请求http://IP:port/abc/123.png时,那么在服务器里面对应的真正的资源是:/data/www/123.png
注意:alias真实路径是alias指定的值,不包含location指定的值了。