Skip to content
Advertisement

Issues with static files – hosting PHP app under subdirectory using nginx

I’m part of a new team on an existing product and the PO wishes for a demo version available under domain.com/demo. The webserver has an nginx setup configured by the previous team.

I am not very familiar with nginx but followed this tutorial in order to setup a demo environment. It seems to be working except that static files (images, css) are still served from the production environment.

We are using a symlinked automated demployment system under /var/www/app/current and /var/www/demoapp/current. The nginx config looks like this:

server {
    server_name www.domain.com;
    return 301 $scheme://domain.com$request_uri;
}
server {
    server_name domain.com [IP_ADDRESS];
    root /var/www/app/current/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    error_page 404 /index.php;

    location ~ .php$ {
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }

    # Media: images, icons, video, audio, HTC
    location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

    # CSS and Javascript
    location ~* .(?:css|js|woff|woff2|ttf|otf|eot)$ {
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }

    location ^~ /demo {
        alias /var/www/demoapp/current/public;
        try_files $uri $uri/ @demo;

        location ~ .php$ {
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }
        # Media: images, icons, video, audio, HTC
        location ~* .(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
            expires 1M;
            access_log off;
            add_header Cache-Control "public";
        }
        # CSS and Javascript
        location ~* .(?:css|js|woff|woff2|ttf|otf|eot)$ {
            expires 1y;
            access_log off;
            add_header Cache-Control "public";
        }
    }

    location @demo {
        rewrite /demo/(.*)$ /demo/index.php?/$1 last;
    }
    ...gzip & certbot configuration
}

When I SSH in to the server, I can see our automated deployment system is working and the new CSS that should be available on the website is compiled under /var/www/demoapp/current/public/css/. However when I visit the domain.com/demo it shows the same CSS stylesheet as the live version.

There was also an issue with the demo version not updating PHP and HTML on deployment to the server. This I fixed by reloading the FPM service on every deployment, however I suspect it might also have to do with the nginx configuration.

Any help for either of these issues would be greatly appreciated.

Advertisement

Answer

The issue ended up not being nginx related. It was fixed by changing every occurance of

<link href="{{ mix('css/dashboard.css') }}" rel="stylesheet">

to

<link href="{{ asset('css/dashboard.css') }}" rel="stylesheet">.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement