首页主机资讯Ubuntu PHP配置与跨域资源共享

Ubuntu PHP配置与跨域资源共享

时间2025-11-25 18:25:03发布访客分类主机资讯浏览1148
导读:Ubuntu 下 PHP 与 CORS 的完整配置指南 一 环境准备与 Apache 启用模块 安装组件(以 Apache + PHP 为例):sudo apt update && sudo apt install apa...

Ubuntu 下 PHP 与 CORS 的完整配置指南

一 环境准备与 Apache 启用模块

  • 安装组件(以 Apache + PHP 为例):sudo apt update & & sudo apt install apache2 php libapache2-mod-php
  • 启用 headers 模块以输出跨域响应头:sudo a2enmod headers
  • 重启服务:sudo systemctl restart apache2
  • 说明:若使用 Nginx,CORS 通常在 server 配置中通过 add_header 设置;下文提供示例。

二 在 PHP 中设置 CORS 响应头

  • 最简示例(开发环境):
<
    ?php
header("Access-Control-Allow-Origin: *");
    
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
    
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");


if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    
    http_response_code(200);
    
    exit;

}
    
// 你的业务逻辑
  • 生产建议:将 * 替换为明确的来源,例如:header(“Access-Control-Allow-Origin: https://example.com”);
  • 多域名白名单示例:
<
    ?php
$allow = ['https://a.example.com','https://b.example.com'];
    
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';

if (in_array($origin, $allow, true)) {
    
    header("Access-Control-Allow-Origin: $origin");

}
    
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
    
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
     http_response_code(200);
     exit;
 }
    
  • 要点:
    • 预检请求(OPTIONS)需直接返回 200,避免业务代码执行。
    • 使用通配符 * 时,浏览器不允许同时携带凭据(如 cookies);需要凭据时请指定具体来源并配合 withCredentials。

三 在 Web 服务器层配置 CORS

  • Apache 虚拟主机或目录配置(推荐在 或对应 VirtualHost 中设置):
<
    Directory /var/www/html>
    
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
<
    /Directory>
    
  • 使用 .htaccess(需 AllowOverride All):
<
    IfModule mod_headers.c>
    
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With"
<
    /IfModule>

RewriteEngine On
RewriteCond %{
REQUEST_METHOD}
 OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
  • Nginx 示例:
server {
    
    listen 80;
    
    server_name yourdomain.com;

    location / {
    
        add_header 'Access-Control-Allow-Origin' '*' always;
    
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
    
        add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With' always;

        if ($request_method = 'OPTIONS') {
    
            add_header 'Access-Control-Allow-Origin' '*';
    
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
    
            add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With';
    
            add_header 'Content-Length' 0;
    
            add_header 'Content-Type' 'text/plain;
     charset=utf-8';
    
            return 200;

        }
    
        # 反向代理或静态文件处理
        try_files $uri $uri/ =404;

    }

}
    
  • 提示:服务器层配置会对匹配范围内的所有资源生效;若需细粒度控制,优先在 PHP 或框架层设置。

四 常见问题与排查要点

  • 预检失败(HTTP 403/405):确保对 OPTIONS 返回 200,并在服务器或 PHP 中输出与方法、头一致的 Access-Control-Allow-* 头。
  • 凭据请求失败:当请求使用 withCredentials 时,不能使用通配符 *,需明确指定 Access-Control-Allow-Origin,并确保 Access-Control-Allow-Credentials: true 已设置。
  • 多域名白名单:使用 $_SERVER[‘HTTP_ORIGIN’] 判定来源并动态设置头,避免硬编码。
  • 静态资源跨域:若静态文件(如图片、字体)也跨域,请在服务器层为对应目录或 location 配置 CORS 头。
  • 配置未生效:确认修改的是正确的 VirtualHost.htaccess 生效路径,并已启用 headers 模块;修改后重载服务(Apache: sudo systemctl reload apache2;Nginx: sudo systemctl reload nginx)。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Ubuntu PHP配置与跨域资源共享
本文地址: https://pptw.com/jishu/755799.html
Ubuntu PHP配置与文件上传限制 如何更新Ubuntu PHP版本

游客 回复需填写必要信息