nginx 来自文件系统中完全不同文件夹的不同位置

nginx different locations from totally different folder in filesystem

我目前正在使用 nginx.

为一个域名提供 Application

我需要在特定的 location 中提供完全不同的 Application,但是第二个 App 位于文件系统中完全不同的文件夹。

感谢this very well explained answer,我了解rootalias之间的区别,所以我选择使用alias

配置文件类似于:

server {
  server_name myapp.dev;
  root /home/apps/myapp/build;
  error_log /home/apps/omg-errors.log warn;
  index index.htm index.html;
  error_page 403 404 /404.html;

  location /admin {
    alias  /home/apps/admin/;
  }
}

我已经尝试使用 root,删除指向路径末尾的 /admin 部分,还尝试在 [=17] 末尾添加尾随 / =],例如:

location /admin/ {
  ...
  # also this:
  root /home/apps/;
}

重要且好奇的部分

是相同的配置在我的本地机器上使用 nginx 工作得很好,配置如下:

server {
  listen 80;
  server_name myapp.local;
  root /Users/lio/Projects/MyApp/my-app-static/build;
  error_log /Users/lio/Projects/MyApp/myapp-error.log warn;

  index index.htm index.html;

  location /admin {
    alias /Users/lio/Desktop/test-admin;
  }
}

日志

以下是尝试访问 /admin 路径时捕获的 error-logs

2020/02/26 04:16:57 [error] 5704#0: *7619 open() "/home/apps/myapp/build/admin" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: myapp.dev, request: "GET /admin HTTP/2.0", host: "myapp.dev"

环境

不知道 OS 和软件版本是否不同,但这里是 nginx -V:

提供的详细信息

本地

nginx version: nginx/1.15.10
built by clang 10.0.1 (clang-1001.0.46.3)
built with OpenSSL 1.0.2r  26 Feb 2019
TLS SNI support enabled
configure arguments: --prefix=/usr/local/Cellar/nginx/1.15.10 --sbin-path=/usr/local/Cellar/nginx/1.15.10/bin/nginx --with-cc-opt='-I/usr/local/opt/pcre/include -I/usr/local/opt/openssl/include' --with-ld-opt='-L/usr/local/opt/pcre/lib -L/usr/local/opt/openssl/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-compat --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module

服务器

nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-google_perftools_module --add-module=/builddir/build/BUILD/nginx-1.14.0/passenger-5.3.7/src/nginx_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

在您的第二个代码段中,您还应该提供 /admin 文件夹,如下所示:

location /admin/ {
  ...
  # also this:
  root /home/apps/admin;
}

否则,要访问管理区域,您需要输入您的域。tld/admin/admin(作为最后一个 /admin)是您的 root /home/apps 声明中缺少的 /admin

这对你有用吗?

经过多次尝试/捕捉,我找到了窍门,那就是 ^~

location ^~ /admin/ {
  alias /home/apps/admin/;
}

简单但(至少对我而言)棘手。

Here are 两个 articles/posts 帮助我更好地理解 location 问题 .