eubnara / study

6 stars 2 forks source link

NGINX 에서 도메인명에 따른 http reverse proxy 실습 #124

Open eubnara opened 5 years ago

eubnara commented 5 years ago

https://gist.github.com/hermanbanken/96f0ff298c162a522ddbba44cad31081

eubnara commented 5 years ago

There is already included stream module in the image(nginx:1.16.0-alpine) which I will use. --with-stream

/ # nginx -V
nginx version: nginx/1.16.0
built by gcc 8.2.0 (Alpine 8.2.0) 
built with OpenSSL 1.1.1b  26 Feb 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer' --with-ld-opt=-Wl,--as-needed
eubnara commented 5 years ago

nodejs

localhost:8080 hello.js

$ cat hello.js 
const express = require('express')
const app = express()
const port = 8080

app.get('/', (req, res) => res.send('Hello'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

localhost:9090 world.js

$ cat world.js 
const express = require('express')
const app = express()
const port = 9090

app.get('/', (req, res) => res.send('World'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))
eubnara commented 5 years ago

nginx

$ docker run --rm --init --network=host -d --name my_nginx -v my_nginx:/etc/nginx nginx:1.16.0-alpine
2528e055f3ec42cc3fe39be303a144de4fa0eb470edd7ab9b52abbd96f41eb61
$ docker exec my_nginx cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    upstream hello {
        server localhost:8080;
    }

    upstream world {
       server localhost:9090;
    }

    server {
    listen 80;
    server_name hello.localhost;
    location / {
        proxy_pass http://hello;
    }
    }

    server {
    listen 80;
    server_name world.localhost;
    location / {
        proxy_pass http://world;
    }
    }

}
eubnara commented 5 years ago

testing

$ curl hello.localhost:80/
Hello
$ curl world.localhost:80/
World
eubnara commented 5 years ago

좋은 예제: https://whatididtodowhatidid.wordpress.com/2014/03/14/subdomains-for-ports-on-same-ubuntu-server-with-nginx-reverse-proxy/