RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1551823
Accepted
FroggerProgger
FroggerProgger
Asked:2023-11-17 20:41:12 +0000 UTC2023-11-17 20:41:12 +0000 UTC 2023-11-17 20:41:12 +0000 UTC

为什么会出现403 Forbidden错误?

  • 772

在 ubuntu 20.04 上安装了 nginx 服务器。目标是确保发送到 localhost/tunnel/ 的所有内容都发送到位于 /var/www/tunnel/index.php 的 index.php 文件

另外,如果请求发送到 localhost/,那么所有内容都会重定向到 localhost:8080

第二个就没有问题了。一切都被重定向。但是1m就出现问题了。

出现错误 403。同时,对隧道文件夹及其所有内容的访问权限为 777。

nginx 设置

server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
    }

    location /tonnel {
        alias /var/www/tonnel; #Используем alias для указания пути к файлам
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
    }
}

文件夹权限截图

访问权

代码index.php

<?php

$mysqli = new mysqli("localhost", "root", "root", "Tonnel");
$connection = $mysqli->connect("localhost", "root", "root", "Tonnel");

if ($connection) {
    print("Wow");
}
else {
    print("bad");
}
php
  • 2 2 个回答
  • 42 Views

2 个回答

  • Voted
  1. Sararun
    2023-11-19T00:59:56Z2023-11-19T00:59:56Z

    所以,可能会有很多问题,这里有近似的解决方案。确保 Nginx 用户和组(通常是 www-data)对 /var/www/tunnel 目录及其所有子目录和文件具有读取权限。从安全角度来看,使用 777 是不好的做法。

    在位置块中使用别名而不是根有时会导致问题。使用 root 的配置将是这样的:

       location /tonnel {
           root /var/www;
           try_files $uri $uri/ /tonnel/index.php$is_args$args;
       }
    

    设置 PHP 文件的位置块以包含 /tunnel。如果不这样做,nginx将无法处理/tunnel目录中的PHP文件。

       location ~ /tonnel/.+\.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
           fastcgi_param SCRIPT_FILENAME $request_filename;
       }
    

    要准确找出 403 错误的原因,请检查 nginx 错误日志,该日志通常位于 /var/log/nginx/error.log 中。他们可能会提供更具体的线索来说明到底是什么导致了问题。好吧,或者如果您使用 laravel,请检查 laravel.log

    尝试重启nginx

    sudo systemctl 重新启动 nginx

    我不太明白为什么,但出于某种原因我不喜欢你的 nginx 配置。

    这是我的 Laravel 项目的配置。请注意,我没有像您那样描述index.php,我将它放在公共目录中。

    server {
        listen 80;
        index index.php index.html;
        root /var/www/public;
    
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Content-Type-Options "nosniff";
    
        charset utf-8;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass app:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
    
        location ~ /\.ht {
            deny all;
        }
    
        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
    }
    
    • 0
  2. Best Answer
    FroggerProgger
    2023-11-24T13:54:58Z2023-11-24T13:54:58Z

    我也尝试在家里做同样的事情。令人惊讶的是,家里一切都很顺利。这次我按照文章的建议做了一切。我的设置几乎不需要改变。您所要做的就是添加索引和根。

    server {
        listen 80;
        server_name your_domain.com;
        root /var/www;
        index index.php;
    
        location / {
            proxy_pass http://192.168.10.33:8080;
            proxy_set_header Host $host;
        }
    
        location /tonnel {
          try_files $uri $uri/ /tonnel/index.php$is_args$args;
       }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    

    屏幕截图显示错误不再存在

    此后错误消失。

    • 0

相关问题

  • mysqli 类的对象无法转换为字符串

  • 您的系统中缺少 ext-http *,您的系统中缺少 ext-mysql_xdevapi *

  • 如何从csv中删除bom?

  • 当我按下 Enter 键时,如何让 PhpStorm 的 Emmet 插件触发,就像 VS Code 一样?

  • 注释在 Symfony5 中不起作用

  • 搜索最近的地理位置点

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5