重写nginx位置规则

时间:2017-04-08 20:57:20

标签: nginx configuration cluster-computing load-balancing nextcloud

我正致力于群集上的负载平衡。它工作得很好,但我意识到我希望能够通过在url中指定它来请求某个节点,例如domain.com/nodeX/request_uri/其中nodeX是我想要发送请求的实际节点。我想这样做的原因是,我可以很容易地知道我在哪个节点,如果我正在其中一个节点上工作,并且需要同步这个节点上的实际文件文件更改时到其他节点。

现在,它只在NextCloud服务器上运行,位于/nextcloud/文件夹中,其中包含与glusterfs共享的数据目录,因此它是{s}}不是我需要复制的这些文件,而是下一个云"的核心文件。或实际上www-directory中任何被更改的文件。

这是(通常)主节点上的设置,`/ etc / nginx / sites-available / default:

upstream cluster {
    ip_hash;
    server node1;
    server node2;
    [...]
    server nodeX; 
}

server {
    listen 443 ssl http2 default_server;
    [...]more unrelated configurations[...]

    # This works as expected        
    location / {
        proxy_pass http://cluster/;
    }

    # But this is where I need help:
    # If location starts with /nodeX, where X is a number
    location ^~ /node([0-9]+) {
        # If location is master node (node0)
        location /node0 {
            # Include nextcloud configuration
            include snippets/nextcloud.conf;
        }

        # Otherwise pass it on to the requested node
        proxy_pass http://«node[0-9]+»/;
    }
}

每个从属节点(nodeX, X > 0)加载相同的配置,这是对它的总结:

server {
    listen 80 default_server; #Yep, no need for SSL in local network
    [...]
    include snippets/nextcloud.conf;
}

我删除了不相关的数据(例如add_headerroot等)以保持清晰。每个节点(包括主节点)共享相同的snippet - 文件夹,该文件夹通过glusterfs分发。此文件snippet/nextcloud.conf是我需要帮助的文件。如果我写domain.com/nextcloud/,下一个云会自动重定向到domain.com/node0/nextcloud/,因此我需要一个解决方案来诱骗服务器相信它在/nextcloud/上运行,只要它{&#39}。实际上是在nodeX的子目录中运行。

这是我到目前为止所做的,它重定向我:

location ~ /(node[0-9]/?)nextcloud {
    # set max upload size
    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    # This is where I should be able to trick the 
    # server to think its running on /nextcloud/ even
    # when its request is /nodeX/nextcloud

    location ~ /(node[0-9]/?)nextcloud {
        rewrite ^ /nextcloud/index.php$uri;
    }

    location ~ ^/(node[0-9]/?)nextcloud/(?:build|tests|config|lib|3rdparty|templates|data)/ {
        deny all;
    }

    location ~ ^/(node[0-9]/?)nextcloud/(?:\.|autotest|occ|issue|indie|db_|console) {
        deny all;
    }

    location ~ ^/(node[0-9]/?)nextcloud/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+|core/templates/40[34])\.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        #Avoid sending the security headers twice
        fastcgi_param modHeadersAvailable true;
        fastcgi_param front_controller_active true;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ ^/(node[0-9]/?)nextcloud/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri/ =404;
        index index.php;
    }

    # Adding the cache control header for js and css files
    # Make sure it is BELOW the PHP block
    location ~* \.(?:css|js|woff|svg|gif)$ {
        try_files $uri /nextcloud/index.php$uri$is_args$args;
        add_header Cache-Control "public, max-age=7200";
        add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        # Optional: Don't log access to assets
        access_log off;
    }

    location ~* \.(?:png|html|ttf|ico|jpg|jpeg)$ {
        try_files $uri /nextcloud/index.php$uri$is_args$args;
        # Optional: Don't log access to other assets
        access_log off;
    }
}

所以我的问题一般是;是否可以删除" / nodeX /"的URI,或任何其他的东西? :)

请注意!当负载均衡器应该处理实际的平衡时,可能会遗漏/nodeX/部分。

0 个答案:

没有答案