Nginx Server Blocks 設定

Nid: 1156

1 . default

$ grep -v '\s*#' /etc/nginx/sites-available/default

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

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
}

2 . Drupal

$ sudo vi /etc/nginx/sites-available/octaviadata.com
server {
        listen         80;
        listen    [::]:80;
        # SSL configuration
#    listen 443 ssl http2;
#    listen [::]:443 ssl http2;
        root /var/www/html/octaviadata.com;
        index index.php index.html index.htm;
        server_name octaviadata.com www.octaviadata.com;
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    location = /ads.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    # Very rarely should these ever be accessed outside of your lan
    #location ~* \.(txt|log)$ {
    location ~* \.(log)$ {
        allow 192.168.0.0/16;
        deny all;
    }
    # Preventing execution of untrusted PHP
    location ~ \..*/.*\.php$ {
        return 403;
    }
    location ~ ^/sites/.*/private/ {
        return 403;
    }
    # Allow "Well-Known URIs" as per RFC 5785
    location ~* ^/.well-known/ {
        allow all;
    }
    # Block access to "hidden" files and directories whose names begin with a
    # period. This includes directories used by version control systems such
    # as Subversion or Git to store control files.
    location ~ (^|/)\. {
        return 403;
    }
    location / {
        try_files $uri /index.php?$query_string; # For Drupal >= 7
    }
    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }
    # Don't allow direct access to PHP files in the vendor directory.
    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }
    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
#               fastcgi_index   index.php;
        # Block httpoxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        # PHP 7 socket location.
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        # to avoid "Upstream timed out (110: Connection timed out) while reading response header from upstream"
        fastcgi_read_timeout 120;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 256 16k;
    }
    # Fighting with Styles? This little gem is amazing.
    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
        try_files $uri @rewrite;
    }
    # Handle private files through Drupal. Private file's path can come
    # with a language prefix.
    location ~ ^(/[a-z\-]+)?/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;
    }
}

設定反映

$ cd /etc/nginx/sites-enabled
$ sudo ln -s ../sites-available/octaviadata.com
$ sudo nginx -t && sudo nginx -s reload

3 . Wordpress

PHP-FPM 設定変更

$ sudo vi /etc/nginx/sites-available/wpdev.octaviadata.com
server {
        listen         80;
        listen    [::]:80;
        server_name wpdev.octaviadata.com;
        root /var/www/html/wpdev.octaviadata.com;
        index index.php;
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
        location / {
                try_files $uri $uri/ /index.php?$args;
        }
        location ~ \.php$ {
                include fastcgi_params;

                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

設定反映

$ cd /etc/nginx/sites-enabled
$ sudo ln -s ../sites-available/wpdev.octaviadata.com
$ sudo nginx -t && sudo nginx -s reload