使用 PHPStudy Pro 配置 Apache 和 Nginx 实现跨域代理

前端项目请求接口的base_url是当前网站的主域名,为了测试我打包后放在了本地phpstudy_pro配置的服务中,结果导致请求404,因为多人项目我就在本地的Nginx 中做了跨域代理,记录一下

1.Nginx反向代理

打开 当前网站的配置文件 D:\phpstudy_pro\Extensions\Nginx1.15.11\conf\vhosts\0localhost_8081.conf

location / {
    proxy_pass http://192.168.124.47:8089;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # 如果需要支持跨域
    add_header Access-Control-Allow-Origin *;
}

2.Apache反向代理

1.打开文件 D:\phpstudy_pro\Extensions\Apache2.4.39\conf\httpd.conf
放开

 LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_module modules/mod_proxy.so

2.打开当前网站的配置文件 D:\phpstudy_pro\Extensions\Apache2.4.39\conf\vhosts\0localhost_8081.conf

<VirtualHost _default_:8080>
    DocumentRoot "D:/phpstudy_pro/WWW"
    FcgidInitialEnv PHPRC "D:/phpstudy_pro/Extensions/php/php7.3.4nts"
    AddHandler fcgid-script .php
    FcgidWrapper "D:/phpstudy_pro/Extensions/php/php7.3.4nts/php-cgi.exe" .php
    
    ProxyRequests Off  //关闭正向代理

    <Directory "D:/phpstudy_pro/WWW">
        Options FollowSymLinks ExecCGI
        AllowOverride All
        Require all granted
        DirectoryIndex index.php index.html
    </Directory>

    <Directory "D:/phpstudy_pro/WWW/workspace">
        Options FollowSymLinks
        AllowOverride All
        Require all granted

        RewriteEngine On
        RewriteBase /workspace/
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /workspace/index.html [L]
    </Directory>

    # ✅ 只保留 ProxyPass / ProxyPassReverse 等允许在 Location 中的指令
    <LocationMatch "^/(api|upload|client|feishu)(/.*)?$">

        ProxyPass http://192.168.124.20:8089/
       
    </LocationMatch>

    ErrorDocument 400 /error/400.html
    ErrorDocument 403 /error/403.html
    ErrorDocument 404 /error/404.html
    ErrorDocument 500 /error/500.html
    # 其他错误页...
</VirtualHost>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值