前言
在涉及到使用Nginx代理多个Apache2项目时,不可避免需要Apache2使用不同的端口才能正常访问。
下面是配置详细步骤(亲自测试)
步骤
1.修改Apache2监听端口
Apache2的监听端口配置文件为 {ports.conf}
|
1 |
vim /etc/apache2/ports.conf |
增加自己需求的端口:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# 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的 8081 和 8082,https的 444 和 445 。
2.修改Apache的网站配置文件
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<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的网站配置文件
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#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.载入配置
|
1 2 |
systemctl restart apache2 systemctl restart nginx |
此时应该就可以访问网站了。(PS:以上是亲自测试过,完美运行)



