0

I have a folder in a domain that is being forward by a Load Balancer (LB), ex: http://domain.name/project-qa, and need to setup nginx to received this request and process it to a Craft CMS instalation.

From what I read, there are some problems with the alias function on nginx, so after a while searching I got to this configuration, using root instead of alias:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/project/public/;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name domain.name;

    location /project-qa/ {
        root /var/www/html/project/public/;
        rewrite ^/project-qa/(.*)$ /$1 break;
   #    try_files $uri $uri/ /project-qa/index.php;

        location ~ \.php$ {
           rewrite ^/project-qa/(.*)$ /$1 break;
           fastcgi_split_path_info ^(.+\.php)(/.+)$;

           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        }
     }

  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  }
}

With this I'm currently able to serve static files, but any request that should be directed to the index.php file (for example, http://domain.name/project-qa/admin) gives the same twig_error_runtime error.

I had this setup in Apache before with Alias and was working (so the rules on the LB are working), but the stack now needs to be in nginx, and I'm having problems setting it up.

The Apache vhost config was:

<VirtualHost *:80>
  DocumentRoot "/var/www/html/project/public/assets"
  ServerName domain.name
  Alias /project-qa /var/www/html/project/public
  <Directory "/var/www/html/project/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

The DocumentRoot was pointing to assets but was never used, since the base domain is never used in this server, only the full http://domain.name/project-qa (except for the LB ping to check if the server is online).

Any help welcome.

  • Does the Craft CMS use a front-controller pattern? How was it configured in Apache? – Tero Kilkanen Mar 16 '17 at 17:06
  • I've added the Apache config to the main post. Yes, all the requests go through the index.php that's located on the public folder. – Proença Mar 16 '17 at 17:22
0

How about simply using this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/project/public/;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name domain.name;

    location /project-qa/ {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;

        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    }
}
| improve this answer | |
  • When using this config I get a nginx 404 message. I've looked into the error.log with debug on and this part looks pertinent for this: 2017/03/17 08:36:38 [debug] 16583#16583: *7 trying to use file: "/project-qa/index.php" "/var/www/html/project/public/project-qa/index.php" 2017/03/17 08:36:38 [debug] 16583#16583: *7 trying to use file: "=404" "/var/www/html/project/public=404" – Proença Mar 17 '17 at 8:40
  • Where is the index.php file then? – Tero Kilkanen Mar 17 '17 at 9:43
  • In the /var/www/html/project/public/ folder. – Proença Mar 17 '17 at 9:55
  • I updated the answer to reflect new information. – Tero Kilkanen Mar 17 '17 at 10:16
  • Hey, thanks, tried that but that gives me the twig_error_runtime error again. One thing that worked, but it's not ideal, was leaving the try_files $uri $uri/ /project-qa/index.php;, and moving the index.php file from the public folder to public/project-qa/, and then adding a ../ to the craft path config in the index.php file. But it's not ideal, since it will need to be a folder there, named for every server type folder (live, staging, etc). But it might help understanding what might be causing it. – Proença Mar 17 '17 at 10:25

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.