gpt4 book ai didi

laravel - 如何设置 nginx laravel + vue cli 项目

转载 作者:行者123 更新时间:2023-12-04 19:10:58 28 4
gpt4 key购买 nike

嗨,我正在为 BackOffice 和 Api 为前端创建 laravel。

在前端,我使用 vuejs。

如何设置 nginx

  • 如果找到路径/admin -> 进入 laravelproject
  • if find path/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

    最佳答案

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

    不要忘记在启用的站点中生成符号链接(symbolic link)
    # 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 配置等等,

    关于laravel - 如何设置 nginx laravel + vue cli 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56556702/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com