Escriu per cercar…

Nginx

Nginx ademés d'un servidor web també és un proxy invers.

S'ensenya a
Seguretat i alta disponibilitatServidors ProxyASIX

Introducció

Un proxy invers (“reverse proxy”) fa referència a un servidor encarregat de mostrar contingut d’un o més servidors que alhora romanen ocults del client o usuari.

Un dels “reverse proxy” més utilitzats com a frontal web és nginx.

Crea una màquina Windows Subsystem for Linux (WSL).

Location

Crea un fitxer de configuració nginx.conf:

nginx.conf
server {
    listen 80;

    location / {
        proxy_pass http://example.com;
    }
}

La directiva server configura nginx perquè escolti al port 80.

La directiva proxy_pass dins de [location](https://nginx.org/en/docs/http/ngx_http_core_module.html#location, configura nginx per passar la sol·licitud a un servidor intermediari HTTP.

La funcionalitat de proxy invers s’habilita a través de la directiva proxy_pass.

Amb aquesta configuració, obrir un navegador a la màquina virtual serà el mateix que obrir un navegador a http://example.com.

Arrenca un servidor nginx amb Docker:

shell
docker run --name nginx -d -p 80:80 -v $(pwd)/nginx.conf:/etc/nginx/conf.d/default.conf:ro nginx:1.25

Pots veure que nginx es configura amb un fitxer de només lectura:

shell
docker logs nginx

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: can not modify /etc/nginx/conf.d/default.conf (read-only file system?)

Obre un navegador a http://localhost:

El contingut és el mateix que el de la pàgina http://example.com.

HTTP Headers

Totes les sol·licituts i respostes HTTP inclouen capçaleres que contenen informació.

Response

Si mires les capçaleres de les respostes localhost i example.com són identiques respecte el contingut:

shell
curl -I localhost

HTTP/1.1 200 OK
Server: nginx/1.25.5
Date: Mon, 02 Jun 2025 09:56:49 GMT
Content-Type: text/html
Connection: keep-alive
ETag: "84238dfc8092e5d9c0dac8ef93371a07:1736799080.121134"
Last-Modified: Mon, 13 Jan 2025 20:11:20 GMT
Cache-Control: max-age=1641

Estàs llegint una vista prèvia.

Inicia sessió amb Google per llegir la pàgina completa.

Inicia sessió amb Google

Amb qualsevol compte de Google. Només et demanarem que acceptis les condicions del servei.