ngnix php8.0-fpm 主要脚本未知

问题描述 投票:0回答:1

我有一个 nginx 服务器,并在 /var/www/main-site 中添加一个 laravel 项目,地址为 site.com 我在/var/www/blog 中添加了一个wordpress 项目。我想用此地址显示博客 site.com/blog。 这是我的 nginx 配置

    listen 80;
    server_name site.com www.site.com;
    root /var/www/main-site/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;
    error_log /var/log/nginx/error.log;

    charset utf-8;

    location /blog {
            alias /var/www/blog;
            try_files $uri $uri/ /index.php?$query_string;

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

    location / {
        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 /index.php;

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

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

当我致电 site.com 时,它可以工作并显示 laravel 项目。但是当我调用 site.com/blog 时,我看到“找不到文件”,状态为 404。并在 nginx 日志中显示此错误

2024/04/30 22:12:16 [error] 3577550#3577550: *7 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: xxx.xxx.xx.xx, server: site.com, request: "GET /blog/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.0-fpm.sock:", host: "site.com"

php nginx server config
1个回答
0
投票

尝试:

location ^~ /blog {
    root /var/www;
    try_files $uri $uri/ /blog/index.php?$query_string;

    ...
}

location
语句需要
^~
运算符,否则
/blog/index.php
将被错误的
location ~ \.php$
块处理。

您应该使用

root
而不是
alias
- 在可以的地方

try_files语句的

最后一个参数
是未找到文件时采取的操作。您是想转至
/index.php
进行其他申请,还是留在
/blog/index.php

© www.soinside.com 2019 - 2024. All rights reserved.