【问题标题】:Apache conf to nginx conf to avoid CORSApache conf 到 nginx conf 以避免 CORS
【发布时间】:2015-06-30 09:03:31
【问题描述】:

在我们的本地开发设置中,我们有一个基于 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。

提前致谢。

【问题讨论】:

    标签: angularjs apache nginx


    【解决方案1】:

    我可以使用 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/;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 2021-06-26
      • 2013-08-10
      • 2021-07-18
      • 2015-11-23
      • 2020-02-06
      相关资源
      最近更新 更多