nginx를 사용하여 역방향 프록시를 설정하는 방법은 무엇입니까?

nginx를 사용하여 역방향 프록시를 설정하는 방법은 무엇입니까?

내 Docker 컨테이너의 호스트 이름을 얻으려고 합니다.역방향 프록시만 사용할 수 있기 때문에이를 위해 nginx의 도움으로 정확히 그것을 달성하려고 노력하고 있습니다.

하나의 도커 컨테이너는 포트 8080을 내 로컬 호스트에 노출하는 웹 서비스입니다.

따라서 다음을 통해 웹 서버에 액세스할 수 있습니다.

http://localhost:8080

대신 다음을 사용하고 싶습니다.

http://webservice.local

그래서 나는 내/etc/hosts

127.0.0.1 webservice.local

그런 다음 nginx를 설치하고 다음에 추가했습니다 /etc/nginx/sites-available/default.

server {
     listen 80 default_server;
     listen [::]:80 default_server ipv6only=on;

     root /usr/share/nginx/html;
     index index.html index.htm;

     # Make site accessible from http://localhost/
     server_name localhost;

     location / {
             # First attempt to serve request as file, then
             # as directory, then fall back to displaying a 404.
             try_files $uri $uri/ =404;
             # Uncomment to enable naxsi on this location
             # include /etc/nginx/naxsi.rules
     }


     location webservice.local {
         proxy_pass http://localhost:8080
     }

nginx를 다시 로드한 후 브라우저에서 ERR_CONNECTION_REFUSED열려고 하면 다음 오류가 발생합니다 .http://webservice.local

내가 뭘 잘못했나요? 역방향 프록시를 올바르게 설정하려면 어떻게 해야 합니까?

답변1

이것이 올바른 구문인지 잘 모르겠습니다. 다음과 같이 시도해 보세요.

upstream myupstream {
    server 127.0.0.1:8080 fail_timeout=2s;
    keepalive 32;
}

location / {
     proxy_pass http://myupstream;
     proxy_redirect http://myupstream/ /;
}

이 라인에 뭔가 ..

하지만 포트 8080을 80으로 리디렉션하려는 경우 socat과 같은 네트워크 유틸리티를 사용하는 것은 어떨까요?

그런 다음 각 업스트림에 대해 nginx에 가상 호스트를 추가하고 DNS 또는 /etc/hosts에 해당 가상 호스트를 추가해야 합니다. 그러면 모두 localhost로 확인됩니다.

아니면 업스트림을 피하고 다음과 같이 가상 호스트를 사용할 수도 있습니다.

server {
  listen 80;
  server_name myvirtualhost1.local;
  location / {
    proxy_pass http://127.0.0.1:8080;
}

server {
  listen 80;
  server_name myvirtualhost2.local;
  location / {
    proxy_pass http://127.0.0.1:9090;
}

관련 정보