Laravel/Nginx: 403 禁止访问

huangapple go评论70阅读模式
英文:

Laravel/Nginx: 403 forbidden

问题

I have two webapps on my server, the first one is WordPress and the second one is Laravel. I need my WordPress site to work on the TLD (domain.com) while the Laravel site to work on a specific path (domain.com/path). Below is my nginx configuration:

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

}

Now when I try to access domain.com/path, I am getting a 403 error as below:

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"

However, if I try to access domain.com/path/index.php, the Laravel site works properly. Also, please note that the WordPress site (domain.com) is working properly with no issues.

P.S. All permissions and ownership have been checked and are configured properly.

英文:

I have two webapps on my server, the first one is WordPress and the second one is Laravel. I need my WordPress site to work on the TLD (domain.com) while the Laravel site to work on specific path (domain.com/path). Below is my nginx configuration:

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

Now when I try to access domain.com/path , I am getting 403 as below:

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"

However, if I try to access domain.com/path/index.php the Laravel site works properly. Also please note that the WordPress site (domain.com) is working properly with no issues.

P.S.
All permissions and ownership checked and they are configured properly.

答案1

得分: 1

以下是翻译好的部分:

"Assume your folder permissions set to correct. This conf should be able to serve your Laravel in domain sub path perfectly and without affect by wordpress."

"假设您的文件夹权限设置正确。此配置应能够完美地为您的Laravel服务于域子路径,而不受WordPress的影响。"

英文:

Assume your folder permissions set to correct.

This conf should be able to serve your Laravel in domain sub path perfectly and without affect by 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;
    }
}

huangapple
  • 本文由 发表于 2023年5月18日 01:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定