0

I am currently serving an Application for one domain name using nginx.

I need to serve a completely different Application in a specific location, but that second App is located in a completely different folder in the filesystem.

I understand the differences between root and alias thanks to this very well explained answer so I opted to use alias

The config file looks something like:

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/;
  }
}

I already tried using root, removing the /admin part at the end of the path pointing to and also tried adding the trailing / at the end of the location, like:

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

The important and curious part

is that the same configuration works pretty well using nginx in my local machine where the config looks like:

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;
  }
}

Logs

Here are the error-logs catched when trying to access the /admin path:

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"

Environment

Don't know if could be the fact of different OS and software versions, however here are the details provided by nginx -V:

Local

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

Server

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'
Lionel T
  • 1,559
  • 1
  • 13
  • 30

2 Answers2

0

On your second snippet, you should also provide the /admin folder, as follow:

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

Otherwise, to access the admin area, you'd need to type yourdomain.tld/admin/admin (as the last /admin) is the missing /admin in your root /home/apps declaration.

Would that suffice to work for you?

Hugo Regibo
  • 697
  • 1
  • 9
  • 20
  • Thank you @hugo-regibo. I show the second snippet just to let know that also tried all the combinations using `root` and `alias` but continues failing. I'll update the _question_ adding the `error-logs` that shows that the `server` is trying files from the main `root` in the server block. Thanks anyway! – Lionel T Feb 26 '20 at 10:16
0

After lots of try / catch I found the trick, and that is ^~.

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

Simple but (at least for me) tricky.

Here are two articles/posts that helped me to understand better the location issue.

Lionel T
  • 1,559
  • 1
  • 13
  • 30