Перейти до основного вмісту

Nginx як reverse proxy для Apache

Коли потрібно, щоб частина вебсервісів працювала на nginx, а частина на apache, можна налаштувати nginx, як проксі, котрий прийматиме запрос та перемикати потоки.

Тобто деякі вебсервіси можуть працювати через nginx, тому що він швидший, а частина через apache.

Причому для HTTPS з використанням сертифікатів, отриманих через certbot, можна вказати порти 443 та 8443 відповідно, а шлях на сертифікат та ключі треба вказати у налаштуваннях як nginx так і apache.

Головні налаштування, наприклад

для nginx у /etc/nginx/sites-available/domain
server {
server_name domain.ua;
listen 80;
if ($host = domain.ua) {
    return 301 https://$host$request_uri;
} # managed by Certbot
}
server {
    server_name domain.ua;
     listen 443 ssl http2;
     ssl_certificate /etc/letsencrypt/live/domain.ua/fullchain.pem; # managed by Certbot
     ssl_certificate_key /etc/letsencrypt/live/domain.ua/privkey.pem; # managed by Certbot
location / {
    proxy_pass https://127.0.0.1:8443;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-Proto $scheme;
     }
}

та для apache у /etc/apache2/sites-available/domain.conf <IfModule mod_ssl.c>
<VirtualHost *:8443>
ServerAdmin user@domain.ua
ServerName domain.ua
DocumentRoot /var/www/domain
<Directory "/var/www/domain">
    Require all granted
     DirectoryIndex index.php
     Options FollowSymLinks
     AllowOverride Limit Options FileInfo
</Directory>
ErrorLog ${APACHE_LOG_DIR}/domain-error_log
CustomLog ${APACHE_LOG_DIR}/domain-access.log combined
# managed by Certbot
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/domain.ua/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.ua/privkey.pem
</VirtualHost>
</IfModule>


Добре описано у

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-18-04-server

Простіше тут

https://www.techrepublic.com/article/how-to-use-nginx-as-a-reverse-proxy-for-apache/


Можливі помилки при роботі тут

https://www.metricfire.com/blog/understanding-nginx-502-bad-gateway-php-fpm/

Коментарі