在Apache上搭建反向代理
首先
当你想要快速地进行HTTP端口转发时,你可以在Apache上设置反向代理。
假设端末通过反向代理(Apache)连接到Web服务器。
请理解,由于使用OCI提供的Oracle Linux,因此存在一些非本质性的步骤(例如停止firewalld和禁用SELLinux)。假设名称解析可用。
准备好了
准备两个实例。
创建Web服务器
使用OCI从Oracle Linux镜像创建实例。
# OCI提供のoracle linux デフォルトイメージでは、firewalldが最初から有効になっている。
# ありがたいことなのだが検証用途では面倒なのでいったんOFFする
[root@web-server ~]# systemctl stop firewalld
# apacheを入れる
[root@web-server ~]# yum install httpd
# httpd サービスを起動する
[root@web-server ~]# systemctl start httpd
# 判別用のファイルを置いておく
[root@web-server ~]# echo "you reached the Web server." > /var/www/html/test.html
# この時点で自分自身からは正しく参照できる
[root@web-server ~]# curl http://localhost:80/test.html
you reached the Web server.
创建代理服务器
同样使用OCI,在Oracle Linux镜像上创建实例。
由於默認情況下SELinux是開啟的,所以需要將其禁用。
如果不禁用它,當嘗試將其傳輸到Web服務器時將失敗並產生503錯誤。
# デフォルトでSELinuxがONになっている
[root@proxy-server ~]# getenforce
Enforcing
[root@proxy-server ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: disabled
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
# SELinuxを無効化する。SELINUX=enforcingからdisabledに修正
[root@proxy-server ~]# vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled ★
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
# 設定反映のためリブート
[root@proxy-server ~]# reboot
# SELinuxが無効化された
[root@proxy-server ~]# getenforce
Disabled
[root@proxy-server ~]# sestatus
SELinux status: disabled
他和之前一样,将firewalld禁用并启动httpd。
# OCI提供のoracle linux デフォルトイメージでは、firewalldが最初から有効になっている。
# ありがたいことなのだが検証用途では面倒なのでいったんOFFする
[root@proxy-server ~]# systemctl stop firewalld
# apacheを入れる
[root@proxy-server ~]# yum install httpd
# httpd サービスを起動する
[root@proxy-server ~]# systemctl start httpd
Apache的反向代理设置
在这个问题中,将Apache作为反向代理设置在代理服务器上。在/etc/httpd/conf/httpd.conf文件中进行以下附加:
·当发送一个curl http://proxy-server:8080/test.html请求时,会被转发到http://web-server:80/test.html。
[root@proxy-server ~]# tail /etc/httpd/conf/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Listen 8080
<VirtualHost *:8080>
ProxyPreserveHost On
ProxyPass / http://web-server:80/
ProxyPassReverse / http://web-server:80/
</VirtualHost>
我参考了下面这个链接:
重启httpd服务以使更改生效。
# httpd再起動
[root@proxy-server ~]# systemctl restart httpd
# 転送用ポート8080がlistenされるようになった。
[root@proxy-server ~]# netstat -anp | grep httpd
tcp6 0 0 :::8080 :::* LISTEN 5636/httpd
tcp6 0 0 :::80 :::* LISTEN 5636/httpd
unix 2 [ ACC ] STREAM LISTENING 52773 5638/httpd /etc/httpd/run/cgisock.5636
unix 2 [ ] DGRAM 50844 5636/httpd
unix 3 [ ] STREAM CONNECTED 52736 5636/httpd
确认
一切问题都已成功进行了端口转发。
PS > curl http://proxy-server:8080/test.html
StatusCode : 200
StatusDescription : OK
Content : you reached the Web server.
RawContent : HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 28
Content-Type: text/html; charset=UTF-8
Date: Sun, 10 Dec 2023 01:55:27 GMT
ETag: "1c-60c1d8e0fe68c"
Last-Modified: Sun, 10 Dec 2023 01:15:...
Forms : {}
Headers : {[Accept-Ranges, bytes], [Content-Length, 28], [Content-Type, text/html; charset=UTF-8], [Date, Sun, 10 Dec 2023 01:55:27 GMT], [ETag, "1c-60c1d8e0fe68c"],
[Last-Modified, Sun, 10 Dec 2023 01:15:37 GMT], [Server, Apache/2.4.37 (Oracle Linux)]}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 28
准备考试
如果想要实现”当发送curl http://proxy-server:8080/hoge/test.html请求时,会被转发到http://web-server:80/fuga/test.html”,可以按照以下方式进行设置。
[root@proxy-server ~]# tail /etc/httpd/conf/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Listen 8080
<VirtualHost *:8080>
ProxyPreserveHost On
ProxyPass /hoge http://web-server:80/fuga
ProxyPassReverse /hoge http://web-server:80/fuga
</VirtualHost>
[root@web-server ~]# echo "you reached the Web server:/fuga/test.html." > /var/www/html/fuga/test.html
PS I> curl http://proxy-server:8080/hoge/test.html
StatusCode : 200
StatusDescription : OK
Content : you reached the Web server:/fuga/test.html.
RawContent : HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 44
Content-Type: text/html; charset=UTF-8
Date: Sun, 10 Dec 2023 02:06:15 GMT
ETag: "2c-60c1e3cf219d5"
Last-Modified: Sun, 10 Dec 2023 02:04:...
Forms : {}
Headers : {[Accept-Ranges, bytes], [Content-Length, 44], [Content-Type, text/html; charset=UTF-8], [Date, Sun, 10 Dec 2023 02:06:15 GMT], [ETag, "2c-60c1e3cf219d5"],
[Last-Modified, Sun, 10 Dec 2023 02:04:31 GMT], [Server, Apache/2.4.37 (Oracle Linux)]}
Images : {}
InputFields : {}
Links : {}
ParsedHtml : mshtml.HTMLDocumentClass
RawContentLength : 44