Comment utiliser nginx ou apache comme reverse proxy?

Apache

sudo a2enmod proxy_http proxy rewrite ssl
systemctl restart apache2
nano /etc/apache2/sites-enabled/000-default.conf (ou votre fichier de configuration que vous avez fait)
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ServerName votresite.com
        ProxyPass / http://localhost:port/
        ProxyPassReverse / http://localhost:port/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Ensuite

systemctl restart apache2

Si vous n’avez pas déjà fait le certificat SSL :

apt install certbot python-certbot-apache2
certbot certonly -d votredomaine.com -d www.votredomaine.com -m votremail@site.com --agree-tos --webroot --webroot-path /var/www/html

Une fois le certificat SSL de générer il faut donc modifier la configuration web.

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ServerName votresite.com
        ServerName www.votresite.com
        RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:443>

    ServerName votresite.com
    ServerAlias www.votresite.com
    Protocols h2 http/1.1

    Header always set Strict-Transport-Security "max-age=63072000"

    DocumentRoot /path/to/files/

    <Directory /path/to/files>
        Options -Indexes
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:port/
    ProxyPassReverse / http://localhost:port/

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/votresite.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/votresite.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/votresite.com/chain.pem
    SSLHonorCipherOrder on
    SSLProtocol             all -TLSv1.2 -TLSv1.3
    SSLProxyEngine on
    SSLCompression off
    SSLOptions +StrictRequire
    SSLCipherSuite ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"

    LogLevel warn
    ErrorLog ${APACHE_LOG_DIR}/www.domain.tld-error.log
    CustomLog ${APACHE_LOG_DIR}/www.domain.tld-access.log combined

</VirtualHost>

Ensuite une fois les modifications finie il faut redémarrer à nouveau Apache

systemctl restart apache2

Nginx

Pour nginx c’est tout aussi simple :

Allez dans le fichier de configuration de nginx qui ce trouve soit dans /etc/nginx/conf.d/votrefichier.conf ou bien /etc/nginx/sites-enabled/votrefichier.conf

server {
    listen 80;
    listen [::]:80;
    server_name votresite.com;
    #return 301 https://$server_name$request_uri;
    # La partie commentée au dessus servira à la redirection vers https
}

Ensuite il faut recharger le fichier de configuration nginx (s’il est déjà existant)

systemctl reload nginx

Sinon

systemctl restart nginx

Il faut générer faire le certificat SSL

certbot certonly -d votredomaine.com -d www.votredomaine.com -m votremail@site.com --agree-tos --webroot --webroot-path /var/www/html

Maintenant que le certificat SSL est généré, il faut de nouveau modifier la configuration de l’hôte virtuel.
Allez dans le fichier de configuration de nginx qui ce trouve soit dans /etc/nginx/conf.d/votrefichier.conf ou bien /etc/nginx/sites-enabled/votrefichier.conf

server {
    listen 80;
    listen [::]:80;
    server_name votresite.com;
    return 301 https://$server_name$request_uri;
}

upstream nomduproxy{
    server localhost:port;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.votresite.com votresite.com;

    access_log /var/log/nginx/site-access.log;
    error_log  /var/log/nginx/site-error.log error;

    # allow larger file uploads and longer script runtimes
    client_max_body_size 100m;
    client_body_timeout 120s;

    sendfile off;

    # SSL Configuration

    ssl_certificate /etc/letsencrypt/live/votresite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/votresite.com/privkey.pem; 
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    ssl_session_cache shared:SSL:10m;
   ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
    ssl_prefer_server_ciphers on;

    add_header Strict-Transport-Security "max-age=15768000; preload;";
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header Content-Security-Policy "frame-ancestors 'self'";
    add_header X-Frame-Options DENY;
    add_header Referrer-Policy same-origin;

    location / {
        include proxy_params;
        proxy_pass http://lenomduproxy;
    }
}

Ensuite il faut recharger le fichier de configuration nginx

systemctl reload nginx

Et voilà vous savez maintenant comment utiliser nginx ou apache comme reverse proxy