Sunday, March 5, 2017

Nginx laravel configuration

I am currently developing a laravel application, on a nginx webserver, I have always accessed my projects by editing /etc/hosts and adding an entry for the name of the project then adding a server block with server name in default file in nginx, so if I have a project called "Missouri", I would call it like this :

http://missouri/

I would like now to change this is a bit, and to use my IP Address, or my localhost to be before the project name, like this :

http://localhost/missouri/

I have searched a lot, found a lot of different combinations, but none was efficient, this is the configuration block for the general server configuration :

server {
    listen 80 default_server;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location ^~  /Missouri {
    alias /var/www/html/Missouri/public;

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

         location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
         }

    }
}

If I use the previous configuration and call localhost/Missouri I get a File not found. white page error, however if I use the following code and call Missouri/ it works :

server {
listen 80;

server_name missouri;

root /var/www/html/Missouri/public;
index index.php index.html;

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

location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

I would appreciate any help, thank you.



via Omar K. Rostom

Advertisement