Question

How To configure Vuejs and Laravel on nginx

I have followed the documentation to deploy Laravel and also Vuejs on digitalocean. I have a project with Laravel backend and Vuejs frontend, now what I want is when visit the site Vuejs file should be load and when I call mysite.com/api/ a Laavel api should work this is my nginx please any idea about this?

server {
    listen 80;
    server_name www.mysite.com mysite.com;
    root /var/www/html/accounting-vue/dist;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location /api/ {
        root /var/www/html/accounting/public;
    }

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

    error_page 404 /;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Fixed, I change the nginx conf file to this

server {
    listen 80;
    server_name www.mysite.com mysite.com;
    root /var/www/accounting/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;
    
    location / {
        root /var/www/accounting-vue/dist;
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    error_page 404 /;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Hi @sarkhelsaeed,

If I understood correctly, you are using both VueJS and Laravel as a whole application however you are using VueJS only for the frontend but still located in Laravel. This would mean you’ll need to configure this in your Laravel routes rather than on your WebService level, in your case Nginx.

Another solution might be creating a redirect from your VueJS files to Laravel if someone opens the /api/ url.

Regards, KDSys