在windows下搭建前后端分离的php项目

前后端分离的web项目,一般会使用同一个域名访问前端网页和后端接口地址,这样可以避免跨域的问题。

Apache配置:

Listen.conf  配置2个监听端口,80用来访问前端网页,801用来访问后端接口

Listen 80
Listen 801


web_80.conf

<VirtualHost *:80>
    DocumentRoot "D:/site/web"
    ServerName xxx.com    
    ProxyPass /api/ http://xxx.com:801/api/
    <Directory "D:/site/web">
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
        DirectoryIndex index.php index.html error/index.html
        Header set Access-Control-Allow-Origin *
    </Directory> 
</VirtualHost>

web_api_801.conf

<VirtualHost *:801>
    DocumentRoot "d:/web/api"
    ServerName xxx.com
    FcgidInitialEnv PHPRC "C:/php/php7.2.13nts"
    AddHandler fcgid-script .php
    FcgidWrapper "C:/php/php7.2.13nts/php-cgi.exe" .php
    <Directory "d:/web/api">
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
        DirectoryIndex index.php index.html error/index.html
        Header set Access-Control-Allow-Origin * 
        Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
        Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
    </Directory>
</VirtualHost>

apache的重定向配置:


        RewriteEngine On
RewriteBase /

RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]



有话要说