Laravel/Nginx:403禁止

bwleehnv  于 2023-05-19  发布在  Nginx
关注(0)|答案(2)|浏览(220)

我的服务器上有两个Web应用程序,第一个是WordPress,第二个是Laravel。我需要我的WordPress网站上的TLD(domain.com)工作,而Laravel网站上的特定路径(domain.com/path)工作。下面是我的nginx配置:

server {
    listen 80;
    server_name domain.com www.domain.com;
    root /home/domain/root3;

    access_log /var/log/nginx/domain_access.log;
    error_log /var/log/nginx/domain_error.log;

    location / {
        root /home/domain/root3;
        index index.php index.html;
        try_files $uri $uri/ /index.php?$args;
    }
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    error_page 404 /index.php;

    location /path {
        alias /home/domain/laravel/public;
        try_files $uri $uri/ /index.php?$query_string;

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

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    client_max_body_size 500M;

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

现在,当我尝试访问domain.com/path时,我得到403如下:

2023/05/17 17:30:49 [error] 26216#26216: *185 directory index of "/home/domain/laravel/public/" is forbidden, client: X.X.X.X, server: domain.com, request: "GET /path/ HTTP/1.1", host: "domain.com"

但是,如果我尝试访问domain.com/path/index.php,Laravel网站可以正常工作。另外请注意,WordPress网站(domain.com)工作正常,没有任何问题。
P.S.检查所有权限和所有权,并正确配置。

gojuced7

gojuced71#

您可以尝试以下步骤:
1.检查目录权限:确保/home/domain/laravel/public目录及其内容具有正确的权限。该目录应可由Web服务器用户读取。您可以使用以下命令设置正确的权限:
sudo chmod -R 755 /home/domain/laravel/public
1.更新Nginx配置:修改Nginx配置中的location /path块如下:

location /path {
     alias /home/domain/laravel/public;
     index index.php;

     try_files $uri $uri/ /path/index.php?$query_string;

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

1.重启Nginx:
sudo service nginx restart
希望能帮上忙

已更新

server {
    listen 80;
    server_name domain.com www.domain.com;
    root /home/domain/root3;

    access_log /var/log/nginx/domain_access.log;
    error_log /var/log/nginx/domain_error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    error_page 404 /index.php;

    location /path {
        alias /home/domain/laravel/public;
        index index.php;

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

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

    location / {
        # WordPress configuration here
        # ...
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    client_max_body_size 500M;

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

}

vuv7lop3

vuv7lop32#

假定您的文件夹权限设置为正确。
这个conf应该能够完美地为您的Laravel提供域子路径,而不会受到WordPress的影响。

server {
    listen 80;
    server_name domain.com www.domain.com;
    root /home/domain/root3;

    access_log /var/log/nginx/domain_access.log;
    error_log /var/log/nginx/domain_error.log;

    location / {
        root /home/domain/root3;
        index index.php index.html;
        try_files $uri $uri/ /index.php?$args;
    }
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    error_page 404 /index.php;

    # Update this section.
    location /path {
        alias /home/domain/laravel/public;
        try_files $uri $uri/ @path;

        location ~ \.php$ {
            fastcgi_pass unix:/run/php/php8.1-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include fastcgi_params;
        }
    }
        
    location @path {
        rewrite /path/(.*)$ /path/index.php?/$1 last;
    }
    # --------------------

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }

    client_max_body_size 500M;

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

相关问题