drupal 8 nginx setup

Events happening in the community are now at Drupal community events on www.drupal.org.
ludo1960's picture

Hi guys,

new to nginx so following blindly like a sheep,

using https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/ I created an example.com in /etc/nginx/sites-available/ and symlinked it from sites-enabled and have my drupal 8 in /var/www/example.com. When I go to eample.com I get a 403 but my test.php shows up just fine in example.com/test.php as does my example.com/adminer.php so it's nearly right. Is there something I need to change in nginx.conf to get drupal 8 to work?

PS everything in var/www/ is owned by www-data, so I dont know what else to try?

Thanks

Comments

What do the access and error

nateb's picture

What do the access and error logs say when you visit the site root?

How are you specifying what and where the default page is? What does it look like around try_files $uri /index.php?$query_string;

Posting your .conf here may help others help you.

more info

ludo1960's picture

Ok more info:

My cunning plan was to get this installed

https://www.contentacms.org/

All goes well until:

server {
server_name contenta.local.com;
root /var/www/contenta.local.com; ## <-- Your only path reference.

seems my problem is similar to https://groups.drupal.org/node/518846

when i go to http://contenta.local.com i get

No input file specified.

when I go to http://contenta.local.com/web/index.php i see the site but none of the links work. I tried changing the server to /var/www/contents.local.co/web but the result is the same.(with nginx restart)

location / {
    # try_files $uri @rewrite; # For Drupal <= 6
    try_files $uri /index.php?$query_string; # For Drupal >= 7
}

location @rewrite {
    rewrite ^/(.*)$ /index.php?q=$1;
}

if I go to http://contenta.local.com/web no index.php shows up, I have to append it to the url, Entries in the location block for D6 and D7 but nothing for drupal 8. What's the magical line of code to get drupal 8 working?

The only lines of I changed in the https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/ are the top 2, ie server name and root.

Any ideas guys?

oops forgot to post nginx error log:

2018/10/12 13:56:58 [notice] 8814#8814: signal process started
2018/10/12 13:59:08 [error] 8818#8818: *253 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /web/?0 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 13:59:08 [error] 8818#8818: *253 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /web/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 13:59:09 [error] 8818#8818: *256 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /profiles/contrib/contenta_jsonapi/modules/contenta_enhancements/logo.svg HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 13:59:09 [error] 8818#8818: *256 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /web/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 14:01:03 [error] 8818#8818: *296 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com"
2018/10/12 14:02:10 [error] 8818#8818: *306 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /web/index.php/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 14:02:19 [error] 8818#8818: *306 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /web/index.php/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 14:09:56 [error] 8818#8818: *376 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /admin/content HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 17:09:11 [error] 643#643: *1017 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com"
2018/10/12 17:09:25 [error] 643#643: *1017 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /web/?0 HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"
2018/10/12 17:09:25 [error] 643#643: *1017 FastCGI sent in stderr: "Unable to open primary script: /var/www/contenta.local.com/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: contenta.local.com, request: "GET /profiles/contrib/contenta_jsonapi/modules/contenta_enhancements/logo.svg HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.0-fpm.sock:", host: "contenta.local.com", referrer: "http://contenta.local.com/web/index.php"

You say the path reference is

nateb's picture

You say the path reference is root /var/www/contenta.local.com; but you navigate to http://contenta.local.com/web/index.php to see the page?

Looks like a root mismatch. /web is a subdirectory and where you're index.php is actually found, no?

Maybe it ought to be root /var/www/contenta.local.com/web;

tried that

ludo1960's picture

As I said in my previous post, tried that:

"when I go to http://contenta.local.com/web/index.php i see the site but none of the links work. I tried changing the server to /var/www/contents.local.co/web but the result is the same.(with nginx restart)"

not touched the nginx.conf file, so not sure what to try next.

Progress!

ludo1960's picture

Update:

prepended /web to a couple of locations in the contenta.local.com.conf file, reloaded nginx and cleared caches

location / {
    # try_files $uri @rewrite; # For Drupal <= 6
    try_files $uri /web/index.php?$query_string; # For Drupal >= 7
}

location @rewrite {
    rewrite ^/(.*)$ /web/index.php?q=$1;
}

now I can log in, result! Still missing the theme tho. Making progress!

If someone who knows more about nginx than my scatter gun approach, now is the time to chip in and update the drupal 8 conf file on nginx web site, so others users can benefit. I switched from apache2 on my little dev box and boy am i glad. The speed difference is obvious.

Following up:

ludo1960's picture

Following up on the so called nginx recipe from:
https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/

server {
server_name example.com;
root /var/www/example.com; ## <-- Your only path reference.

Using the above code the webisite throws a "No input file specified. "

When I change it to:

server {
server_name example.com;
root /var/www/example.com/web; ## <-- Your only path reference.

The website is reachable, but all the image paths are all wrong with pre-installed content and hitting the myaccount link results in Access denied, the link http://example.com/index.php/user should be http://example.com/index.php/user/1 so the drupal 8 site is expecting the root to be /var/www/example.com and not /var/www/example.com/web, there has to be something wrong with official nginx drupal recipe. I must be missing a re-write rule or something like that, Any ideas guys?

The official nginx Drupal

nateb's picture

The official nginx Drupal recipe won't work for all situations, but that doesn't mean there's something wrong with it.

We don't know enough about what's in your site's .conf file or it's settings.php, or how the site was configured to diagnose the root cause.

I'd recommending trying to set up a local site in a way that matches the expectations of the official nginx recipe. Test with a fresh installation of Drupal 8 using the default build.

When that works, move on to modifying the locations of paths in .conf and testing.

When that works, move on to trying the same with Contenta.

ok started anew, installed

ludo1960's picture

ok started anew, installed drupal 8 as suggested, no problems whatsover. then onto the updated contenta. Started with the official nginx repo, same issues as before, tried https://github.com/contentacms/contenta_docker/blob/master/nginx/conf/dr... for nginx conf but get a 404 not found, here's what i changed to get get sudo nginx -t to run:

https://github.com/contentacms/contenta_docker/blob/master/nginx/conf/drupal.conf

server {
listen 80;

fastcgi_keep_conn on;
root /var/www/contenta.local.com/web;
index index.php;

### Nginx configuration for Drupal 7 and 8. This configuration makes use of
### drush (http:///drupal.org/project/drush) for site maintenance
### and like tasks:
###
### 1. Run the cronjobs.
### 2. Run the DB and code updates: drush up or drush upc followed by
### drush updb to run any DB updates required by the code upgrades
### that were performed.
### 3. Disabling of xmlrpc.xml and update.php: all updates are now
### handled through drush.

## If ssl terminator had ssl, we're passing that on
if ($http_x_forwarded_proto = 'https') {
set $fastcgi_https "on";
}

## rewriting /index.php to / because after https://www.drupal.org/node/2599326
## autocomplete URLs are forced to go to index.php
rewrite ^/index.php / last;

## The 'default' location.
location / {

  ## Disallow access to any dot files
  location ~ /\. {
      deny all;
      access_log off;
      log_not_found off;
  }

  ## Replicate the Apache <FilesMatch> directive of Drupal standard
  ## .htaccess. Disable access to any code files. Return a 404 to curtail
  ## information disclosure. Hide also the text files.
  location ~* ^(?:.+\.(?:htaccess|make|txt|yml|md||engine|inc|info|install|module|profile|po|pot|sh|.*sql|test|theme|tpl(?:\.php)?|xtmpl)|code-style\.pl)$ {
      deny all;
      access_log off;
      log_not_found off;
  }

  ## Expiring per default for four weeks and one second, Drupal will overwrite that if necessary
  expires 2628001s;

  ## First we try the URI and relay to the /index.php?q=$escaped_uri&$args if not found.
  try_files $uri @drupal;

}

########### Security measures ##########

## Restrict access to the strictly necessary PHP files. Reducing the
## scope for exploits. Handling of PHP code and the Drupal event loop.
location @drupal {
include /etc/nginx/fastcgi.conf;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
### fastcgi_pass ${NGINX_FASTCGI_PASS:-php}:9000; ### Changed to line above
}

## Trying to access private files directly returns a 404.
location /sites/default/files/private/ {
internal;
}

## Disallow access to patches directory.
location ^~ /patches {
deny all;
access_log off;
log_not_found off;
}

## Disallow access to backup directory.
location ^~ /backup {
deny all;
access_log off;
log_not_found off;
}

## Disallow access to vagrant directory.
location ^~ /vagrant {
deny all;
access_log off;
log_not_found off;
}

## Disallow access to /core/vendor.
location ^~ /core/vendor {
deny all;
access_log off;
log_not_found off;
}

## Disallow access to /vendor.
location ^~ /vendor {
deny all;
access_log off;
log_not_found off;
}

## Disable access logs for robots.txt.
location = /robots.txt {
access_log off;
## Add support for the robotstxt module
## http://drupal.org/project/robotstxt.
try_files $uri @drupal;
}

location = /humans.txt {
access_log off;
## Add support for the humanstxt module
## http://drupal.org/project/humanstxt.
try_files $uri @drupal;
}

## Support for favicon. Return an 1x1 transparent GIF if it doesn't
## exist.
location = /favicon.ico {
expires 30d;
try_files /favicon.ico @empty;
}

## Return an in memory 1x1 transparent GIF.
location @empty {
expires 30d;
empty_gif;
}

# Any other attempt to access PHP files does not have access.
location ~* ^.+.php$ {
deny all;
}

### Directives for installing drupal.
location = /install.php {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#### changed to line above: fastcgi_pass ${NGINX_FASTCGI_PASS:-php}:9000;
}
### Directives for installing drupal.
location = /core/install.php {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
#### changed to line above fastcgi_pass ${NGINX_FASTCGI_PASS:-php}:9000;
}

}

Sorted

ludo1960's picture

Seems an undocumented settings.local.php needs to placed in the sites folder! All working now! cheers guys!

im using wnmp web server, some links are working

musicster's picture

worker_processes 1;

error_log logs/error.log;
pid logs/nginx.pid;

events {
# Max value 16384
worker_connections 8192;
# Accept multiple connections
multi_accept on;
}

Settings that affect all server blocks

http {
include php_processes.conf;
include mime.types;
default_type application/octet-stream;

access_log  logs/access.log;

sendfile on;

keepalive_timeout  65;
ssl_session_timeout 10m;
ssl_protocols TLSv1.2 TLSv1.1 TLSv1 SSLv3;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!ADH:!AECDH:!MD5:!DSS;
ssl_prefer_server_ciphers on;
gzip  on;
# http server

Begin HTTP Server

server {
listen 80; # IPv4
server_name localhost;

## Parametrization using hostname of access and log filenames.
access_log logs/localhost_access.log;
error_log logs/localhost_error.log;

## Root and index files.
root C:/Wnmp/html/drupal;
index  index.php index.html index.htm;

## If no favicon exists return a 204 (no content error).
location = /favicon.ico {
    try_files $uri =204;
    log_not_found off;
    access_log off;
}

## Don't log robots.txt requests.
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

location @rewrite {
    rewrite ^ index.php?$query_string last;

}

## Try the requested URI as files before handling it to PHP.

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


    ## Regular PHP processing.
    location ~ \.php$ {
        fastcgi_pass   php_processes;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    ## Static files
    location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
    try_files $uri $uri/ @rewrite;
        expires max;
        log_not_found off;
        ## No need to bleed constant updates. Send the all shebang in one
        ## fell swoop.
        tcp_nodelay off;
        ## Set the OS file cache.
        open_file_cache max=1000 inactive=120s;
        open_file_cache_valid 45s;
        open_file_cache_min_uses 2;
        open_file_cache_errors off;
    }

    ## Keep a tab on the 'big' static files.
    location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
        expires 30d;
        ## No need to bleed constant updates. Send the all shebang in one
        ## fell swoop.
        tcp_nodelay off;
    }
    } # / location

}

End HTTP Server

Begin HTTPS Server

server {
listen 443 http2 ssl;
server_name localhost;
ssl_certificate cert.pem;
ssl_certificate_key key.pem;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;

## Parametrization using hostname of access and log filenames.
access_log logs/localhost_access.log;
error_log logs/localhost_error.log;

## Root and index files.
root C:/Wnmp/html/drupal;
index  index.php index.html index.htm;

## If no favicon exists return a 204 (no content error).
location = /favicon.ico {

    log_not_found off;
    access_log off;
}

## Don't log robots.txt requests.
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
location @rewrite {
    rewrite ^ index.php?$query_string last;

}

## Try the requested URI as files before handling it to PHP.
location / {
try_files $uri /index.php?$query_string;


    ## Regular PHP processing.
    location ~ \.php$ {
        fastcgi_pass   php_processes;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    ## Static files are served directly.
    location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
      try_files $uri $uri/ @rewrite;
        expires max;
        log_not_found off;
        ## No need to bleed constant updates. Send the all shebang in one
        ## fell swoop.
        tcp_nodelay off;
        ## Set the OS file cache.
        open_file_cache max=1000 inactive=120s;
        open_file_cache_valid 45s;
        open_file_cache_min_uses 2;
        open_file_cache_errors off;
    }

    ## Keep a tab on the 'big' static files.
    location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
        expires 30d;
        ## No need to bleed constant updates. Send the all shebang in one
        ## fell swoop.
        tcp_nodelay off;
    }
    } # / location

} # End HTTPS Server
}

"Relocated Document Root" impact on apache/nginx root?

GaryWong's picture

Hi

I've been stuck in D6/D7 so when I saw the use of composer and relocated document root, I was excited.

So do we now use ././web root in the webserver config? Or is D8 smart enough to go down one level? I'm using https://github.com/drupal/recommended-project ...

TIA
Gary

Nginx

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: