我有 3 个系统。 main 在 haproxy(即公共(public))中,而其他两个运行 apache 服务器的系统。以下是 conf 代码片段
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 10000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 10000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:80
default_backend apache
#Log Format
log-format %ci:%cp_[%t]_%bi:%bp_%s_%{+Q}r_%ST_%Tq/%Tw/%Tc/%Tr/%Tt_%B
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend apache
balance roundrobin
server apache40 10.11.12.11:80 check
server apache41 10.11.12.12:80 check
在 apache 网络服务器上,有一些网站的 php 代码。当我通过浏览器打开网站时,出现以下错误(通过 inspect element 观察到)
Failed to load http://10.11.12.11/main/assets/redis/new5.php?_=1530007431471: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
(index):1 Failed to load http://10.11.12.11/main/assets/redis/new5.php?_=1530007431472: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
下面的字体文件也是这个问题
Access to Font at 'http://10.11.12.11/main/assets/fonts/fontawesome-webfont.woff?v=4.2.0' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
(index):1 Access to Font at 'http://10.11.12.11/main/assets/fonts/font/nafeesnastaleeqregular1.woff' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
(index):1 Access to Font at 'http://10.11.12.11/main/assets/fonts/fontawesome-webfont.ttf?v=4.2.0' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
(index):1 Access to Font at 'http://10.11.12.11/main/assets/fonts/font/nafeesnastaleeqregular1.ttf' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
问题出在哪里。我进行了很多搜索并尝试了很多解决方案,但注意到对我有用。如果有的话,我应该在 conf 中做什么更改?
最佳答案
您必须在您的 apache 服务器上允许 CORS。
https://enable-cors.org/server_apache.html
有关 CORS 的更多信息:
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
如果您不访问服务器,您可以在浏览器中使用扩展程序允许它,例如用于 chrome 的“CORS 转换器”(我正在使用)
关于php - 与 Apache 集成时的 HAPROXY 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51040289/