首页 > 解决方案 > 在 NGINX 子定位中配置 laravel 时出现问题

问题描述

我的应用程序包含一个仅 vue 的前端,其 api 是用 laravel 编写的。我想这样设置它的方式:

http://myapp.local--> 指向 vue 前端。

http://myapp.local/api--> 指向 laravel 应用 api 路由。

这是我的 nginx 配置:

server {
    listen 80;
    server_name myapp.local *.myapp.local;
    root /var/www/myapp-frontend/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 ^~ /api {
        alias /var/www/myapp-api/public;
        try_files $uri $uri/ @api;

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

     location @api {
        rewrite /api/(.*)$ /api/index.php/$1 last;
     }

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

http://myapp.local/api点击打开根 laravel 路线。但是,如果我在我的应用程序中打开任何其他路由,我会收到500 Internal Server Error

这是 nginx 的错误日志中的错误:

2019/09/09 15:58:18 [error] 20954#20954: *10 rewrite or internal redirection cycle while redirect to named location "@api", client:
127.0.0.1, server: myapp.local, request: "GET /api/admin/features HTTP/1.1", host: "myapp.local"

更新

我设法让它以某种方式工作,这就是更新后的配置

server {
    listen 80;

    server_name *.myapp.local;
    root /var/www/myapp-api/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.php;

    charset utf-8;

    add_header X-debug-request-filename "$request_filename" always;
    add_header X-debug-document-root "$document_root" always;
    add_header X-debug-fastcgi-script-name "$fastcgi_script_name" always;
    add_header X-debug-query-string "$query_string" always;

     location / {
        root /var/www/myapp-frontend/dist;
        try_files $uri $uri/ /index.html = 404;
     }

     location /api {
        alias /var/www/myapp-api/public;
        try_files $uri $uri/ @api;

        add_header X-debug-request-filename "$request_filename" always;
        add_header X-debug-document-root "$document_root" always;
        add_header X-debug-fastcgi-script-name "$fastcgi_script_name" always;
        add_header X-debug-query-string "$query_string" always;

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

     location @api {
        rewrite ^/api/(.*)$  /api/index.php?/$1 last; # THIS IS THE IMPORTANT LINE
     }

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

    #error_page 404 /index.php;

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

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

不过,我有一个小问题,每次访问 api 路由时,我都需要写/api两次才能获得正确的地址。例如像这样http://myapp.local/api/api/login/api/login是实际的路由,并且http://myapp.local/api/指向 laravel 应用程序,所以需要写/api两次才有意义。但是无论如何有可能只使用http://myapp.local/api/login访问/api/login路线吗?

标签: phplaravelnginx

解决方案


location /为 vue.js 解释器添加

server {
    listen 80;
    server_name myapp.local *.myapp.local;
    root /var/www/myapp-frontend/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 ^~ /api {
        alias /var/www/myapp-api/public;
        try_files $uri $uri/ @api;

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

     location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;  
    }

     location @api {
        rewrite /api/(.*)$ /api/index.php/$1 last;
     }

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

在此处使用端口3000作为示例。将其设置为您的节点侦听端口。


推荐阅读