리눅스
Nginx와 Apache 연동하여 프록시 설정하기
변군이글루
2025. 2. 20. 12:45
반응형
Nginx와 Apache 연동하여 프록시 설정하기
이 구성은 Nginx를 프록시 서버로 활용하여 특정 요청(/test/)을 Apache 서버로 전달하는 방식입니다. 이를 통해 Nginx의 고성능 리버스 프록시 기능과 Apache의 웹 처리 능력을 결합하여 더욱 안정적인 웹 서비스를 제공할 수 있습니다.
https://test.scbyun.com
https://test.scbyun.com/test/
테스트 환경
Nginx 서버 : 192.168.10.101
Apache 서버 : 192.168.10.102
Nginx 설정
설정 파일 위치 : /etc/nginx/conf.d/test.scbyun.com.conf
vim /etc/nginx/conf.d/test.scbyun.com.conf
upstream backend {
server 192.168.10.102:443 max_fails=3 fail_timeout=10s;
keepalive 32;
}
server {
listen 80;
server_name test.scbyun.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name test.scbyun.com;
root /usr/share/nginx/html;
index index.html index.htm;
access_log /var/log/nginx/test.scbyun.com-access.log;
error_log /var/log/nginx/test.scbyun.com-error.log;
location /test/ {
rewrite ^/test(/.*)?$ /$1 break;
proxy_pass https://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
ssl_certificate "/etc/nginx/ssl/wildcard_scbyun_com/_wildcard_scbyun_com.crt";
ssl_certificate_key "/etc/nginx/ssl/wildcard_scbyun_com/_wildcard_scbyun_com.key";
location = /404.html {
root /usr/share/nginx/html;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
설정 설명
- upstream backend : Apache 서버(192.168.10.102)를 대상으로 프록시 설정을 정의합니다.
- HTTP(80) 요청을 HTTPS(443)로 리다이렉트합니다.
- HTTPS(443) 요청을 처리하며 /test/ 경로 요청을 Apache 서버로 프록시합니다.
- rewrite를 사용해 /test/ 접두사를 제거하여 요청을 전달합니다.
- proxy_pass를 통해 Apache 서버로 요청을 전달합니다.
- SSL 인증서를 설정하여 보안 통신을 지원합니다.
Apache 설정
설정 파일 위치 : /usr/local/apache2/conf/extra/httpd-ssl.conf
vim /usr/local/apache2/conf/extra/httpd-ssl.conf
<VirtualHost 0.0.0.0:443>
DocumentRoot "/usr/local/apache2/htdocs"
ServerName test.scbyun.com
ErrorLog "|/usr/sbin/cronolog /usr/local/apache2/logs/test.scbyun.com-error-%Y%m%d.log"
CustomLog "|/usr/sbin/cronolog /usr/local/apache2/logs/test.scbyun.com-access-%Y%m%d.log" combined
SSLCertificateFile /usr/local/apache2/conf/ssl/_wildcard_scbyun_com.crt
SSLCertificateKeyFile /usr/local/apache2/conf/ssl/_wildcard_scbyun_com.key
SSLCACertificateFile /usr/local/apache2/conf/ssl/rsa-dv.chain-bundle.pem
</VirtualHost>
설정 설명
- DocumentRoot를 /usr/local/apache2/htdocs로 설정하여 기본 웹 페이지를 제공합니다.
- ServerName을 test.scbyun.com으로 지정하여 해당 도메인 요청을 처리합니다.
- ErrorLog 및 CustomLog를 설정하여 cronolog을 활용한 날짜별 로그 파일을 생성합니다.
- SSL 인증서를 설정하여 HTTPS 통신을 지원합니다.
Apache(192.168.10.102) 서버에서 테스트하는 방법
Nginx 서버로 직접 테스트
curl -L --resolve test.scbyun.com:443:192.168.10.101 https://test.scbyun.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Apache 서버로 직접 테스트
curl -L --resolve test.scbyun.com:443:192.168.10.102 https://test.scbyun.com
<html><body><h1>It works!</h1></body></html>
Nginx를 거쳐 Apache로 보내지는지 테스트
- 1) Nginx에서 301 리다이렉트 응답을 반환하고 2) 새 URL로 Apache에 요청을 보내는 과정을 확인할 수 있습니다.
curl -v -L --resolve test.scbyun.com:443:192.168.10.101 https://test.scbyun.com/test/
* Added test.scbyun.com:443:192.168.10.101 to DNS cache
* About to connect() to test.scbyun.com port 443 (#0)
* Trying 192.168.10.101...
* Connected to test.scbyun.com (192.168.10.101) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
* subject: CN=*.scbyun.com
* start date: Jul 03 00:00:00 2024 GMT
* expire date: Jul 10 23:59:59 2025 GMT
* common name: *.scbyun.com
* issuer: CN=Sectigo RSA Domain Validation Secure Server CA,O=Sectigo Limited,L=Salford,ST=Greater Manchester,C=GB
> GET /test/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: test.scbyun.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Thu, 20 Feb 2025 03:49:14 GMT
< Content-Type: text/html; charset=EUC-KR
< Content-Length: 45
< Connection: keep-alive
< Upgrade: h2
< X-Frame-Options: DENY
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
<
<html><body><h1>It works!</h1></body></html>
* Connection #0 to host test.scbyun.com left intact
curl -L --resolve test.scbyun.com:443:192.168.10.101 https://test.scbyun.com/test/
<html><body><h1>It works!</h1></body></html>
728x90
반응형