---
title: Proxy Reverso
slug: aiops2/proxy-reverso
description: Learn how to configure reverse proxies using NGINX and Apache2 with this comprehensive document. Discover examples of NGINX settings for WebSockets and proxy buffers, as well as Apache2 modules for proxy, rewrite, proxy_http, and proxy_wstunnel. Whether y
docTags: 
createdAt: 2023-10-20T18:17:41.683Z
---

***

### Instalação do Proxy Reverso NGINX

Exemplo de configuração do proxy reverso utilizando o nginx.

```shell
map $http_x_forwarded_proto $protoscheme {
    default $scheme;
    https https;
}

# required for WebSockets
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

upstream ws-simon-bal {
    server 127.0.0.1:80; # IP Master Node
}

server {

    # ...

    keepalive_timeout   60;
    server_tokens off;

    # Strict Transport Security
    add_header Strict-Transport-Security max-age=2592000;

    # proxy buffers
    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    location ~* ^/(ws)/ {
        rewrite  ^/(ws/.*)$ /$1 break;
        proxy_pass         http://ws-simon-bal;
        # proxy_redirect    off;
        proxy_redirect     http://ws-simon-bal/ $scheme://127.0.0.1/;
        proxy_set_header   Host            127.0.0.1;
        proxy_set_header   X-Host          $http_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 $http_host;
        proxy_set_header   X-Forwarded-Proto $protoscheme;

        # required for WebSockets
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 20d;
        proxy_buffering off;
    }

    location / {
        expires -1;
        add_header Cache-Control no-cache;
        add_header X-Proxy-Cache $upstream_cache_status;
        #
        default_type       application/json;
        proxy_cache        main;
        rewrite            ^/(.*)$              /$1 break;
        proxy_set_header   Host                 127.0.0.1;
        proxy_set_header   X-Host               $http_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     $http_host;
        proxy_set_header   X-Forwarded-Proto    $protoscheme;
        proxy_redirect     off;
        proxy_pass         http://ws-simon-bal;
	    # 1 hora ?
        proxy_read_timeout 3600s;
    }
}
```

***

### Instalação do Proxy Reverso Apache2

Exemplo de configuração do proxy reverso para apache com os modulos obrigatórios: (*proxy, rewrite, proxy\_http, proxy\_wstunnel*).

```shell
a2enmod proxy rewrite proxy_http proxy_wstunnel
```

```shell

# Exemplo para porta 80/443
# Redirecionar tráfego da porta 80 -> 443

<VirtualHost *:80>
  ServerName simon.seudominio.com
  # ... Outras configurações

  Redirect permanent / https://simon.seudominio.com
  
</VirtualHost>

<VirtualHost *:443>
  
  ServerName simon.seudominio.com

  # ... Outras configurações

  ProxyRequests Off
  ProxyPreserveHost On

  # Suporte à Websockets
  RewriteEngine On  
  RewriteCond %{REQUEST_URI}   ^/                [NC]
  RewriteRule /(.*)            http://simon.seudominio/$1 [P,L]
  RewriteCond %{HTTP:Upgrade}  =websocket        [NC]
  RewriteCond %{REQUEST_URI}   ^/ws/             [NC]
  RewriteRule /(.*)            ws://simon.seudominio/$1   [P,L]

  # 
  ProxyPass        / http://simon.seudominio/ timeout=600
  ProxyPassReverse / http://simon.seudominio/ timeout=600

</VirtualHost>
```

Exemplo de configuração do proxy reverso.
