【问题标题】:nginx proxy and backbone pushstatenginx 代理和主干推送状态
【发布时间】:2013-02-02 18:51:41
【问题描述】:

我正在尝试设置 nginx 以与我的骨干网应用程序和 api 服务器一起使用。

API 服务器是外部的,并且通过https://website.com/api/...进行路由。

基本上,我希望将任何不匹配的 url 路由到 /index.html 以供主干应用程序处理。

我尝试过使用try_files,但这只是覆盖了我的 API。我尝试设置另一个位置来检查请求是否为 GET,以及它是否与 registerloginapi 不匹配,但这也不起作用。到目前为止,这是我的server

server {
    listen 80; ssl off;
    listen  443 ssl;
    server_name app.io;
    ssl_certificate /etc/nginx/conf/ssl.crt;
    ssl_certificate_key /etc/nginx/conf/app.key;

    root /home/ubuntu/app/public;

    access_log /var/log/nginx/app.access.log;
    error_log /var/log/nginx/app.error.log;

    index index.html;

    location / {
                    if ($scheme = "http") {
                            rewrite ^ https://$http_host$request_uri? permanent;
                    }
            }

    location ~ ^/(api)|(auth).*$ {
        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://app.aws.af.cm;
    }

    location ~ ^(/(register)|(login)).*$ {
        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;
        # GETs only
        limit_except POST {
            proxy_pass https://app.aws.af.cm;
        }
    }

    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        expires max;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

}

目前,try_files 覆盖 API 并仅重定向到 index.html。知道我怎样才能让所有东西都能很好地相互配合吗?

这就是我想要的:

if / - /index.html
else if /api/*|/auth/* - external proxy
else if /login|/register - POST - external proxy
else /* - /#$1

【问题讨论】:

    标签: unix nginx


    【解决方案1】:

    想通了:

    try_files @uri @rewrites; 添加到Location / 并在下面添加@rewrites 函数。

    server {
        listen 80; ssl off;
        listen  443 ssl;
        server_name app.io;
        ssl_certificate /opt/nginx/conf/ssl.crt;
        ssl_certificate_key /opt/nginx/conf/app.key;
    
        root /home/ubuntu/app/public;
    
        access_log /var/log/nginx/app.access.log;
        error_log /var/log/nginx/app.error.log;
    
        index index.html;
    
        location / {
                        if ($scheme = "http") {
                                rewrite ^ https://$http_host$request_uri? permanent;
                        }
    
                                   try_files $uri @rewrites;
                }
    
        location ~ ^/(api)|(auth)|(logout)|(register)|(login).*$ {
            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://app.cm;
        }
    
        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            expires max;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }
    
        location @rewrites {
                rewrite ^/.+ /#$uri redirect;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      相关资源
      最近更新 更多