如何设置Nginx Laravel + Vue CLI项目

时间:2019-06-12 07:23:29

标签: laravel ubuntu nginx vue.js

嗨,我要为BackOffice创建laravel,为前端创建Api。

在前端,我使用vuejs。

如何设置nginx

  • 如果找到路径/ admin->输入laravelproject
  • 如果找到路径/ api->输入laravel项目
  • 否则只需输入vue项目

这是我现在使用的

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

    root /var/www/html;

    index index.html index.php  index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
        // Here is working fine run index.html ( vue )
        try_files $uri $uri/ = /index.html;
    }

    location ~ \.php$ {
      // setup php version
      include snippets/fastcgi-php.conf;

      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
   }

    location  /api {
        // I want to go in laravel path here. It's not working 
         root /var/www/html/serverside/public;
         try_files $uri $uri/ /index.php?$query_string;
    }

    location  /admin {
        // I want to go in laravel path here.It's not working 
         root /var/www/html/serverside/public;
         try_files $uri $uri/ /index.php?$query_string;
    }

}

这是我的文件夹结构

/var/www/html/serverside/laravelproject ( in serverside laravel project locate here )
/var/www/html/index.html ( Here is vue js )

***** 更新 *****

这是我的laravel.conf

server {
    listen 80;
    root /var/www/html/serverside/public;
    index  index.php index.html index.htm;
    server_name  localhost;

    location / {
        try_files $uri $uri/ /index.php?$query_string;        
    }


    location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass             unix:/var/run/php/php7.2-fpm.sock;
       fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

nginx error.log显示 / etc/nginx/sites-enabled/laravel.conf" failed (40: Too many levels of symbolic links) in /etc/nginx/nginx.conf: 62

2 个答案:

答案 0 :(得分:0)

用于laravel的最小nginx虚拟主机是这样的,在ubuntu服务器中,该虚拟主机的正确位置是/ etc / ngixt / sites-available。

#laravel.conf
server {
    listen 80;
    root /var/www/html/project_name/public;
    index  index.php index.html index.htm;
    server_name  api.example.com www.api.example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;        
    }


    location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass             unix:/var/run/php/php7.2-fpm.sock;
       fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

用于vuejs的最小nginx vhost就是这样,您可以永久使用pm2在特定端口(例如8080,8081,...)上运行vue js应用程序...

#vue.conf
server {
  listen 80;
  index index.html;
  server_name example.com www.example.com;

  location / {
      proxy_pass http://localhost:8080;
  }
}

别忘了在启用的网站中生成符号链接

# ln -s /etc/nginx/sites-enabled/laravel.conf /etc/nginx/sites-enabled/
# ln -s /etc/nginx/sites-enabled/vue.conf /etc/nginx/sites-enabled/
# service ntinx -t
# service nginx restart

此外,您可以添加ssl配置以及更多内容,

答案 1 :(得分:0)

如果有人遇到此消息错误:

nginx.service的作业失败,因为控制进程退出并显示错误代码。我无法运行服务nginx restart /etc/nginx/sites-enabled/laravel.conf”(40:符号链接级别过多)

文件链接无法链接同一文件的原因

示例:ln -s /etc/nginx/sites-enabled/laravel.conf / etc / nginx / sites-enabled /

它将显示此消息错误

请使用此命令链接文件

/ etc / nginx / sites-available / {fileName} / etc / nginx / sites-enabled /