Nginx反向代理Apache2多个端口

前言

在涉及到使用Nginx代理多个Apache2项目时,不可避免需要Apache2使用不同的端口才能正常访问。

而且不知道什么原因,好像Apache2开启了HSTS严格认证,使得Nginx只能代理https,代理http的话会出现无法访问的情况。无奈~

这边记录一下学习过程。

步骤

1.修改Apache2监听端口

Apache2的监听端口配置文件为 ports.conf

vim /etc/apache2/ports.conf

增加自己需求的端口:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 8081
Listen 8082

<IfModule ssl_module>
        Listen 444
        Listen 445
</IfModule>

<IfModule mod_gnutls.c>
        Listen 444
        Listen 445
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

此时就监听了http的 80818082 ,https的 444445

2.修改Apache的网站配置文件

<VirtualHost *:8081> 
    DocumentRoot /var/www/***

</VirtualHost>

<VirtualHost *:444>
    DocumentRoot /var/www/***

    SSLEngine on
    SSLCertificateFile      /***/***.crt
    SSLCertificateKeyFile /***/***.key
    SSLCertificateChainFile /***/***.crt

    <Directory /var/www/***>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch "\.(?:cgi|shtml|phtml|php)$">
        SSLOptions +StdEnvVars
    </FilesMatch>
    <Directory /usr/lib/cgi-bin>
        SSLOptions +StdEnvVars
    </Directory>
</VirtualHost>

正常来说,本地的反向代理使用http就行,不建议代理https。

3.修改Nginx的网站配置文件

#https部分配置
server {
    listen 443      ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/***
    server_name yydnas.cn www.yydnas.cn;
    index index.php index.html index.htm;

    ssl_certificate     /etc/xxx/xxx.pem
    ssl_certificate_key /etc/xxx/xxx.key
    ********省略********
    location / {
        proxy_pass_header Server;
        proxy_pass https://localhost:444;  #或者http://localhost:8081
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

4.载入配置

systemctl restart apache2
systemctl restart nginx

此时应该就可以访问网站了。

总结

学习的过程挺好玩的,主要是网上关于apache2的教程不是很多,很多东西都要自己一个个尝试,体验的是一个过程。

0 0 投票数
文章评分
订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x
滚动至顶部