WordPress, Laravel, and NGINX

2019-01-20 06:36发布

问题:

I"m trying to get the configuration for NGINX to work with WordPress being the default such as domain.com and the Laravel app being domain.com/app.

I'm not opposed to it being a sub-domain, but I wanted to try this first. I'm not sure which is easier / better.

The directories are: WP - /var/www/wordpress L5 - /var/www/laravel

server {
        client_max_body_size 10M;
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/wordpress;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name somename;

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

        location = /robots.txt {
                log_not_found off;
                access_log off;
                allow all;
        }

        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                expires max;
                log_not_found off;
        }

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

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

        location /app/ {
                alias /var/www/laravel/public;

                try_files $uri $uri/ /app//app/index.php?$query_string;

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

                }

        }

        location ~ /\.ht {
                deny all;
        }

}

回答1:

I cannot confirm that this will work as I do not know Laravel. Two PHP apps without a common root will need two PHP locations (which you have). However, the location /app/ statement needs to use the ^~ modifier to stop the wrong PHP location from being selected. See this document for details.

location ^~ /app/ {
    alias /var/www/laravel/public/;

    if (!-e $request_filename) { rewrite ^ /app/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        include snippets/fastcgi-php.conf;

        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

I see your question includes a fix for the alias/try_files bug - but I prefer the the if block solution above.