Apache conf到nginx conf以避免CORS

时间:2015-06-30 09:03:31

标签: angularjs apache nginx

在我们的本地开发设置中,我们在Angular JS上构建了一个纯客户端应用程序,它连接到托管在不同服务器上的服务。

为了避免在我们的开发环境中使用CORS,我们将Apache配置为代理,如下所示

#Apache Configuration
<VirtualHost *:*>
DocumentRoot "../apps"
ProxyPreserveHost On

SSLProxyEngine On
SSLProxyVerify none 
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off

ProxyPass /env2/ https://env2-abc.com/
ProxyPassReverse /env2/ https://env2-abc.com/

ServerName localhost:9000
</VirtualHost>

我想为相同的配置设置nginx,但我正面临CORS问题

#nginx configuration
server {
    rewrite_log on;
    listen 9090;
    server_name localhost;
    root ../apps;
    index index.html;

    location /env2 {
        add_header Access-Control-Allow-Origin *;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
        proxy_pass https:///env2-abc.com;           
    }
}

有人可以帮我正确设置代理和反向代理。到目前为止,apache conf运行良好,但我想尝试使用nginx。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我能够使用nginx xonf文件中的以下配置来解决上述问题

server {
    rewrite_log on;
    listen 9000;
    server_name localhost;
    root ../apps;
    index index.html;
    access_log off; # I do not need logging in dev env
    error_log off;  # I do not need logging in dev env

    location /env2/ { # trailing / gets substituted by proxy_pass
        proxy_redirect          off;
        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-Host $host;
        proxy_set_header        X-Forwarded-Server $host;
        proxy_pass              https://env2-abc.com/;
    }
}