informatica:linux:nginx
Differences
This shows you the differences between two versions of the page.
| informatica:linux:nginx [2021/05/21 05:11] – created javi | informatica:linux:nginx [2021/05/21 05:49] (current) – javi | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | HELLO WORLD | + | ===== Obtener IP origen sin terminar conexión SSL ===== |
| + | |||
| + | En este escenario: | ||
| + | |||
| + | * Edge router. Es el nginx que recibe la conexión del cliente, y expone a la DMZ los puertos TCP 443 y TCP 80. Si recibe la conexión al puerto TCP 443 lo redirige (capa 4) a " | ||
| + | * Behind edge. Servidor que expone puertos TCP 443 y TCP 80 a 'edge router' | ||
| + | * Php. Pinta las cabeceras que recibe | ||
| + | |||
| + | Requisitos: | ||
| + | |||
| + | * Docker instalado | ||
| + | * Docker compose instalados | ||
| + | * El servidor tiene que tener una IP pública | ||
| + | * Nombre DNS (en este ejemplo " | ||
| + | |||
| + | 1. Edge router | ||
| + | |||
| + | 1.1. Crear directorio: | ||
| + | |||
| + | mkdir edge | ||
| + | |||
| + | 1.2. Crear ' | ||
| + | |||
| + | vim edge/ | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | proxy_set_header X-Real-IP | ||
| + | proxy_set_header X-Forwarded-For $remote_addr; | ||
| + | # To avoid 404. Credits: | ||
| + | # https:// | ||
| + | proxy_set_header Host $host: | ||
| + | |||
| + | server { | ||
| + | listen 80; | ||
| + | location / { | ||
| + | proxy_pass http:// | ||
| + | } | ||
| + | error_page 404 / | ||
| + | location = /404.html { | ||
| + | root / | ||
| + | internal; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 1.3. Crear ' | ||
| + | |||
| + | vim edge/ | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | user nginx; | ||
| + | worker_processes | ||
| + | |||
| + | error_log | ||
| + | pid / | ||
| + | |||
| + | |||
| + | events { | ||
| + | worker_connections | ||
| + | } | ||
| + | |||
| + | |||
| + | http { | ||
| + | include | ||
| + | default_type | ||
| + | |||
| + | log_format | ||
| + | ' | ||
| + | '" | ||
| + | |||
| + | access_log | ||
| + | |||
| + | sendfile | ||
| + | # | ||
| + | |||
| + | keepalive_timeout | ||
| + | |||
| + | #gzip on; | ||
| + | |||
| + | include / | ||
| + | |||
| + | upstream behind_edge { | ||
| + | # Docker container named ' | ||
| + | server behind_edge: | ||
| + | } | ||
| + | } | ||
| + | |||
| + | stream { | ||
| + | |||
| + | server { | ||
| + | listen 443; | ||
| + | |||
| + | proxy_pass behind_edge_ssl; | ||
| + | proxy_protocol on; | ||
| + | } | ||
| + | |||
| + | upstream behind_edge_ssl { | ||
| + | # Docker container named ' | ||
| + | server behind_edge: | ||
| + | } | ||
| + | |||
| + | log_format basic ' | ||
| + | ' | ||
| + | ''; | ||
| + | |||
| + | access_log | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | 2. Behind edge | ||
| + | |||
| + | 2.1. Crear directorio: | ||
| + | |||
| + | mkdir behind_edge | ||
| + | |||
| + | 2.2. Crear ' | ||
| + | |||
| + | vim behind_edge/ | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | proxy_set_header X-Real-IP | ||
| + | proxy_set_header X-Forwarded-For $remote_addr; | ||
| + | # To avoid 404. Credits: | ||
| + | # https:// | ||
| + | proxy_set_header Host $host: | ||
| + | |||
| + | proxy_set_header X-Custom-IP $proxy_protocol_addr; | ||
| + | proxy_set_header X-Custom-Port $proxy_protocol_port; | ||
| + | |||
| + | server { | ||
| + | listen 80; | ||
| + | location / { | ||
| + | proxy_pass http:// | ||
| + | } | ||
| + | } | ||
| + | |||
| + | server { | ||
| + | listen 443 ssl proxy_protocol; | ||
| + | location / { | ||
| + | proxy_pass https:// | ||
| + | } | ||
| + | ssl_certificate / | ||
| + | ssl_certificate_key / | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 2.3. Crear ' | ||
| + | |||
| + | vim behind_edge/ | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | user nginx; | ||
| + | worker_processes | ||
| + | |||
| + | error_log | ||
| + | pid / | ||
| + | |||
| + | |||
| + | events { | ||
| + | worker_connections | ||
| + | } | ||
| + | |||
| + | |||
| + | http { | ||
| + | include | ||
| + | default_type | ||
| + | |||
| + | log_format | ||
| + | ' | ||
| + | '" | ||
| + | |||
| + | access_log | ||
| + | |||
| + | sendfile | ||
| + | # | ||
| + | |||
| + | keepalive_timeout | ||
| + | |||
| + | #gzip on; | ||
| + | |||
| + | include / | ||
| + | |||
| + | upstream php { | ||
| + | # Docker container named ' | ||
| + | server php:80; | ||
| + | } | ||
| + | |||
| + | upstream php_ssl { | ||
| + | # Docker container named ' | ||
| + | server php:443; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | 2.4. Crear: | ||
| + | |||
| + | vim behind_edge/ | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | FROM nginx | ||
| + | |||
| + | RUN apt-get update && apt-get install -y \ | ||
| + | ssl-cert | ||
| + | </ | ||
| + | |||
| + | 3. Php | ||
| + | |||
| + | **NOTA**: de hecho la parte de SSL en el contenedor PHP ya no es necesaria, pero bueno... | ||
| + | |||
| + | 3.1. Crear directorio: | ||
| + | |||
| + | mkdir php | ||
| + | |||
| + | 3.2. Crear: | ||
| + | |||
| + | vim php/ | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | <?php | ||
| + | |||
| + | foreach (getallheaders() as $name => $value) { | ||
| + | echo " | ||
| + | } | ||
| + | |||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | 3.3. Crear: | ||
| + | |||
| + | vim php/ | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | FROM php: | ||
| + | |||
| + | RUN apt-get update && apt-get install -y \ | ||
| + | ssl-cert | ||
| + | |||
| + | RUN a2enmod \ | ||
| + | remoteip \ | ||
| + | ssl | ||
| + | |||
| + | RUN a2ensite default-ssl.conf | ||
| + | |||
| + | COPY index.php / | ||
| + | </ | ||
| + | |||
| + | 4. Crear: | ||
| + | |||
| + | vim docker-compose.yml | ||
| + | |||
| + | Con el siguiente contenido: | ||
| + | |||
| + | < | ||
| + | services: | ||
| + | |||
| + | behind_edge: | ||
| + | build: | ||
| + | context: ./ | ||
| + | container_name: | ||
| + | volumes: | ||
| + | - "/ | ||
| + | - "/ | ||
| + | |||
| + | edge: | ||
| + | container_name: | ||
| + | image: nginx | ||
| + | ports: | ||
| + | - 80:80 | ||
| + | - 443:443 | ||
| + | volumes: | ||
| + | - "/ | ||
| + | - "/ | ||
| + | |||
| + | php: | ||
| + | build: | ||
| + | context: ./php | ||
| + | container_name: | ||
| + | |||
| + | version: " | ||
| + | </ | ||
| + | |||
| + | Ajustar las rutas a los archivos, en este ejemplo "/ | ||
| + | |||
| + | < | ||
| + | . | ||
| + | ├── behind_edge | ||
| + | │ ├── default.conf | ||
| + | │ ├── Dockerfile | ||
| + | │ └── nginx.conf | ||
| + | ├── docker-compose.yml | ||
| + | ├── edge | ||
| + | │ ├── default.conf | ||
| + | │ └── nginx.conf | ||
| + | └── php | ||
| + | ├── Dockerfile | ||
| + | └── index.php | ||
| + | </ | ||
| + | |||
| + | 5. Probar. Ejecutar: | ||
| + | |||
| + | curl -s -k https:// | ||
| + | |||
| + | Resultado esperado similar a: | ||
| + | |||
| + | < | ||
| + | X-Real-IP: 8.8.8.8< | ||
| + | X-Forwarded-For: | ||
| + | Host: php_ssl< | ||
| + | Connection: close< | ||
| + | User-Agent: curl/ | ||
| + | Accept: */ | ||
| + | </ | ||
| + | |||
| + | Vemos que la cabecera ' | ||
| + | |||
informatica/linux/nginx.1621573900.txt.gz · Last modified: by javi
