0

I will not display the entire file. Everything works in this file except the handling of the /client location, which I setup below:

location / {
     # First attempt to serve request as file, then
     # as directory, then fall back to displaying a 404.
     try_files $uri $uri/ =404;
}

location /client {
     alias /var/www/client/dist;
     try_files $uri $uri/ /var/www/client/dist/index.html =404;
}

I am serving a php application from the /var/www/server directory. It properly operates at this time.

From the /var/www/client directory, I have made an Angular2 application. The command ng build --prod --base-href=/client/ builds the application and stores it in the folder /var/www/client/dist/, which has the index file located at /var/www/client/dist/index.html.

A request for a file that exists at /var/www/client/dist/real_file_path.ext should be made to http://host/client/real_file_path.ext, and a request for a file that does not exist, such as http://host/client/non-existant/directory should be directed to /var/www/client/dist/index.html where the application parses the url to route the view and controler.

The log shows that the alias successfully redirects requests from /client to /client/dist, but it serves a 404 instead of serving /var/www/client/dist/index.html.

What is causing the failure?

I clearly don't understand something about alias or try_files.

I have tried using:

try_files $uri $uri/ /client/dist/index.html =404;

This did not correct the issue either.

After each change, I run sudo nginx -s reload. After each change, I verify the performance of the php application.

2
  • 1
    Can you edit your question to show the access / error logs for a successful and failed request, and also to show permissions of the client folder.
    – Tim
    Jun 7 '18 at 23:45
  • 1
    I guess you should use try_files $uri $uri/ /client/index.html;
    – Alexey Ten
    Jun 8 '18 at 8:17
0

Thank you to @Tim and @Alexey Ten.

When I saw Alexey Ten's comment on the main post, I was sure that was the answer, so I changed it to try_files $uri $uri/ /client/index.html =404;, but this did not fix the problem.

I had a problem with empty access logs, which I had overlooked before since I had plenty in my error logs, but in an effort to gather more information to update the question as suggested by Tim, I learned that I previously changed the location and format of my access logs.

log_format extlog '$request_time - $status - local time: $time_local; root: $document_root; uri: $document_uri; host: $host; hostname: $hostname; realpath root: $realpath_root; client address: $remote_addr; client port: $remote_port; request filename: $request_filename; method: $request_method; request_uri: $request_uri; scheme: $scheme; server_name: $server_name; server_port: $server_port; server_protocol: $server_protocol; response status: $status; uri: $uri';
access_log /var/log/nginx/long_log.log extlog;

This log format is basically a data-dump of the available variables.

What I noticed when I attempted to copy the logs was this:

0.000 - 200 - local time: 08/Jun/2018:09:19:08 -0700; root: /var/www; uri: /server/Core/js/autocomplete.js; host: sandbox.pediatricheartcenter.org; hostname: sandbox; realpath root: /var/www; client address: 192.168.76.37; client port: 54947; request filename: /var/www/server/Core/js/autocomplete.js; method: GET; request_uri: /server/Core/js/autocomplete.js; scheme: http; server_name: sandbox.pediatricheartcenter.org; server_port: 80; server_protocol: HTTP/1.1; response status: 200; uri: /server/Core/js/autocomplete.js
0.000 - 404 - local time: 08/Jun/2018:09:17:29 -0700; root: /var/www/client/dist; uri: /client/visit/5990653da56b4075bff360efcc52383f20171208090130; host: sandbox.pediatricheartcenter.org; hostname: sandbox; realpath root: /var/www/client/dist; client address: 192.168.76.37; client port: 54706; request filename: /var/www/client/dist/visit/5990653da56b4075bff360efcc52383f20171208090130; method: GET; request_uri: /client/visit/5990653da56b4075bff360efcc52383f20171208090130; scheme: http; server_name: sandbox.pediatricheartcenter.org; server_port: 80; server_protocol: HTTP/1.1; response status: 404; uri: /client/visit/5990653da56b4075bff360efcc52383f20171208090130

Specifically, the root is different for the two requests. This means that alias alters the root for requests in the /client location so that my reference needs to be with respect to this new root.

I changed the try_files to reference /index.html, and now it works!

My final location block appears below:

location /client {
    alias /var/www/client/dist;
    try_files $uri $uri/ /index.html =404;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.