首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache conf到nginx conf,以避免CORS

Apache conf到nginx conf,以避免CORS
EN

Stack Overflow用户
提问于 2015-06-30 17:03:31
回答 1查看 706关注 0票数 0

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

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

代码语言:javascript
复制
#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问题

代码语言:javascript
复制
#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。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-07-02 01:36:47

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

代码语言:javascript
复制
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/;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31133930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档