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;
    }
}

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.

×
2 answers

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

  • Hi @KDSys,

    Thanks for your response, VueJS project is not located in Laravel they are in different directory if you notice the nginx conf you see
    VueJs is in /var/www/html/accounting-vue/dist;
    but Laravel is in /var/www/html/accounting/public;
    so I think the problem is nginx cannot find the Laravel when the location is /api, because in browser when I visit mysite.com the VueJs login form will show but when visit mysite.com/api it shows me File not found.

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;
    }
}

Submit an Answer