Nginx:ReverseProxy
This article describes the basic configuration of a proxy server. You will learn how to pass a request from NGINX to proxied servers over different protocols, modify client request headers that are sent to the proxied server, and configure buffering of responses coming from the proxied servers.
Passing a Request to a Proxied Server
When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client. It is possible to proxy requests to an HTTP server (another NGINX server or any other server) or a non-HTTP server (which can run an application developed with a specific framework, such as PHP or Python) using a specified protocol. Supported protocols include FastCGI, uwsgi, SCGI, and memcached.
To pass a request to an HTTP proxied server, the proxy_pass directive is specified inside a location. For example:
opm에서 사용중인 nginx-forward 스크립트
server {
listen 80;
server_name "@FRONTEND_HOST@";
# charset utf-8;
# access_log /var/log/nginx/host.access.log main;
# dns resolver used by forward proxying.
resolver 8.8.8.8;
# Sets the maximum allowed size of the client request body.
client_max_body_size 1024m;
# proxy_connect;
# proxy_connect_allow 443 563;
# proxy_connect_connect_timeout 30s;
# proxy_connect_read_timeout 30s;
# proxy_connect_send_timeout 30s;
# [ERROR]
# location / {
# proxy_pass "@BACKEND_SCHEME@://@BACKEND_HOST@$uri$is_args$args";
# [ERROR]
# location ~ ^(/.*) {
# set $query $1;
# proxy_pass "@BACKEND_SCHEME@://@BACKEND_HOST@$query";
# [OK]
location / {
rewrite ^(/.*)$ $1 break;
proxy_pass @BACKEND_SCHEME@://@BACKEND_HOST@;
proxy_ssl_verify off;
proxy_set_header Host @FRONTEND_HOST@;
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_protocol_addr;
}
}
WebSocket 전달
Troubleshooting
공백(%20) 전달시 400 에러
다음과 같이 사용하는게 가장 일반적이지만, 경로에 공백(%20
)이 포함되면 정상적으로 작동하지 않는다. (확인해보지 않았지만 정규화 되는 과정에서 %20
이 공백으로 바뀌면서, 인자 전달이 잘 안되는듯 하다)
그래서 다음과 같이, $query
변수에 추가하여 적용해서 정상동작을 확인했다.
하지만, MediaWiki 에서 300번대 (정확히는 기억 안남) 상태코드가 확인되면서 CSS 로드가 안되더라 ... -_-;; 그래서 아래와 같이 rewrite 엔진을 적용하서 정상동작 확인함.
Certificate is not trusted
상황은 다음과 같다.
- 아래 내용은 Swarm으로 연동함.
- Traefik으로 Lets encript를 적용한 HTTPS를 전면으로 두고,
- NGINX Reverse Proxy를 사용하여 동일 네트워크 상의 다른 PC 로 접속.
- (다른 PC 도 위와 동일한 환경이지만, 일시적으로 서비스하던 곳에서 떼 와서 직접 연결)
- 다른 PC 또한 Traefik으로 Lets encript를 적용한 HTTPS를 전면으로 두고, 내부에 접속하려는 서비스 (GitLab)가 존재함.
결국 접속 순서는 다음과 같다:
클라이언트(브라우저) --[https]--> Traefik --[https]--> nginx reverse proxy --[https]--> (최종PC의)treafik --[https]--> (최종PC의)gitlab
이 때 각각 다음과 같은 에러가 발생한다. (빨간 박스는 브라우저 익명모드로 접속가능한 방법을 그린거임)
| |
해결 방법은 proxy_ssl_verify off;
을 추가한다: