2016-09-22 11 views
0

私のマシン上で2つのWebサーバーを拒否しました.1つはApache(ポート80)でPHP5.3を起動し、もう1つはNginx(ポート8080)でPHP 7.0.2を起動します。別のローカルNginxサーバーへのプロキシとして機能するApache上でCORSを有効にする方法(またはNginxであるべきですか?)

私はApacheをNginxのプロキシとして動作させるようにしました。


私は設定である下記は、ApacheのVirtualHostの設定:

以下
<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName wxforum.com 
    ServerAlias wxforum.com 

    Header set Access-Control-Allow-Origin "http://wxforum.com" 

    ErrorLog "/private/var/log/apache2/wxforum.com-error_log" 
    CustomLog "/private/var/log/apache2/wxforum-access_log" common 

    ProxyPass/http://127.0.0.1:8080/ 
    ProxyPassReverse/http://127.0.0.1:8080/ 
</VirtualHost> 

はnginxのの部分的な設定です:

server { 

    listen  8080; 
    server_name localhost; 
    set $root_path '/usr/local/var/www'; 
    root $root_path; 
    #index index.php index.html index.htm 

    #charset koi8-r; 

    access_log /usr/local/var/log/nginx/localhost.access.log main; 
    error_log /usr/local/var/log/nginx/localhost.error.log; 


    location/{ 

      index index.php index.html index.htm; 
      try_files $uri $uri/ /index.php$is_args$query_string; 
    } 


    #error_page 404    /404.html; 

    # redirect server error pages to the static page /50x.html 
    # 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root $root_path; 
    } 



    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    location ~ \.php$ { 
     #root   html; 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index /index.php; 

     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include  fastcgi_params; 
    } 

} 

Iアクセスhttp://wxforum.com、Nginxが動作しますが、私はissu e AJAXリクエストで失敗し、私に表示されます:

XMLHttpRequest cannot load http://127.0.0.1:8080/_debugbar/open?op=get&id=9932e2decca12d5f5109a1a61d4ce5dc. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://wxforum.com' is therefore not allowed access.

この場合、純粋なWebサーバーでCORSを有効にするにはどうすればよいですか?

答えて

0

私はUsing CORSCORS on Nginxを読んだ後、私の問題を解決しました。それはApacheの設定ファイルを編集する必要はありません。

server { 
    listen  8080; 
    server_name localhost; 

    set $root_path '/usr/local/var/www'; 
    root $root_path; 

    #charset koi8-r; 

    access_log /usr/local/var/log/nginx/localhost.access.log main; 
    error_log /usr/local/var/log/nginx/localhost.error.log;  
    index index.php index.html index.htm; 


    # enable CORS 
    # http://www.html5rocks.com/en/tutorials/cors/#toc-cors-server-flowchart 
    # http://enable-cors.org/server_nginx.html 
    # http://stackoverflow.com/questions/14499320/how-to-properly-setup-nginx-access-control-allow-origin-into-response-header-bas/29113949#29113949 
    set $allow_origin 'http://wxforum.com'; 

    add_header 'Access-Control-Allow-Origin' $allow_origin; 
    add_header 'Access-Control-Allow-Methods' 'GET, POST'; 
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 


    location/{ 
     # add_header Access-Control-Allow-Origin $cors_header; 
     try_files $uri $uri/ /index.php$is_args$query_string; 
    } 
    ... 
} 

最後に、私のnginx.confは以下のようなものです

関連する問題