3

My goal is to have a Laravel installation running alongside a Nuxt application generated as static content.

I'd like for Laravel to be available, when the location start with /api. This works as expected.

For any other requests, I'd like to have Nginx serve me the static content from inside another folder.

I can achieve this by changing the document root at line 18 (root /var/www/html/public/dist;) and changing the following try_files configuration to what it states in the config below.

I tried switching root out for alias instead, and that gave me some funky results. I get a 500 server response from Nginx with the following output in the error log:

2020/09/29 13:28:17 [error] 7#7: *3 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 172.21.0.1, server: _, request: "GET /my/fake/url HTTP/1.1", host: "localhost"
172.21.0.1 - - [29/Sep/2020:13:28:17 +0000] "GET /claims/creat HTTP/1.1" 500 580 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" "-"

I have the following configuration (running inside a Docker container).

server {
    listen 80 default_server;

    root /var/www/html/public;

    index index.html index.htm index.php;

    server_name _;

    charset utf-8;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt  { log_not_found off; access_log off; }

    error_page 404 /index.php;

    location / {
        alias /var/www/html/public/dist;
        try_files $uri $uri/ /index.html;
        error_page 404 /400.html;
    }

    location ~ /api {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    
    location ~ /\.ht {
        deny all;
    }
}

I'm not entirely sure what the difference is which also makes me question whether I should use alias or root in my case, and I would appreciate some help with understanding this.

Repox
  • 265
  • 1
  • 3
  • 14

1 Answers1

4

The problem from your error message is with your try_files, not your root or alias. You tried to load a URL path that doesn't exist, and in a normal configuration nginx would just serve a 404 or try to load your web app's front controller. But your try_files tells it to serve /index.html instead. So it starts over trying to load /index.html and ends up at the same try_files, but that file doesn't exist either, so it gives the error rewrite or internal redirection cycle while internally redirecting to "/index.html" because it was already trying to load /index.html.

You should first fix try_files. Examples:

try_files $uri $uri/ =404;           # static site
try_files $uri $uri/ /index.php;     # PHP front controller

Now, to your secondary question.

root specifies the actual document root, the directory on the filesystem to serve static files from, which corresponds to the URL path /. For instance, if you have root /var/www/html/public and ask for a URL /css/myapp.css then this will map to the file path /var/www/html/public/css/myapp.css.

alias allows you to remap some URL path below the root to some other directory so that you can serve static files from elsewhere. For example, for a location /static/ you might define alias /var/www/html/files/. In this case, instead of going to a subdirectory of the root, the alias will be substituted for the part of the URL path in the location. So /static/ becomes /var/www/html/files/ and a request for /static/myapp.css will try to load the file /var/www/html/files/myapp.css instead of /var/www/html/public/css/myapp.css.

It doesn't make sense to use alias with location /. If necessary to define a different file path here, use root (but be aware that this can be an anti-pattern).

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Thank you for your answer. I'm not quite sure I understand the `try_files` part though, because it works when I use `root` but not when I use `alias`. The static site works as expected when using `root`. Does that mean my `try_files` is still wrong though? – Repox Sep 29 '20 at 16:03
  • @Repox It's fine if you're serving a single page app, but you never mentioned anything to suggest that you are doing that. – Michael Hampton Sep 29 '20 at 16:21
  • Hi again. Thank you for replying again. My page isn't a single page app, the website is generated with html pages for each possible routes from the static site generator. – Repox Sep 30 '20 at 06:56
  • Then go with what is written above in the answer. – Michael Hampton Sep 30 '20 at 12:22