与Apache集成时的HAPROXY问题

时间:2018-06-26 10:10:57

标签: php linux apache cors haproxy

我有3个系统。主要在haproxy(公共)中运行,而其他两个正在运行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元素观察到)

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中做些什么更改?

1 个答案:

答案 0 :(得分:0)

您必须在您的Apache服务器上允许CORS。

https://enable-cors.org/server_apache.html

有关CORS的更多信息:

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

如果您无法访问服务器,则可以使用扩展程序(例如我正在使用的chrome的“ CORS转换器”)在浏览器中允许它