티스토리 뷰

반응형

 

 

NGINXRocky Linux

 

 

한 서버에 여러 사이트들이 동작중이어서 개발시에 포트로 접근했으나

여러 구성원들이 접근하기 쉽게 nginx 에 다중 도메인 설정을 하여 운영해보려고 한다.

 

Nginx와 서브도메인을 운영한다는 전제하에 기술하겠습니다.

 

 

먼저 nginx 가 없으신 분들은 각 사용하시는 리눅스 설치 명령어로 nginx 를 설치하십시오.

 

dnf install nginx

 

저는 설치된 기본상태에서 진행 하겠습니다.

nginx 를 설치하고 /etc/nginx 경로에 접근하면 아래와 같습니다.

 

drwxr-xr-x. 2 root root  100 Mar 31 09:31 conf.d
-rw-r--r--. 1 root root 1007 Oct 19 19:48 fastcgi_params
-rw-r--r--. 1 root root 5349 Oct 19 19:48 mime.types
lrwxrwxrwx. 1 root root   29 Oct 19 19:48 modules -> ../../usr/lib64/nginx/modules
-rw-r--r--. 1 root root  683 Mar 22 18:02 nginx.conf
-rw-r--r--. 1 root root  636 Oct 19 19:48 scgi_params
-rw-r--r--. 1 root root  664 Oct 19 19:48 uwsgi_params

 

여기서 여러 사이트를 운영할 경우 깔끔하게 설정을 관리하기 위해

sites-available, sites-enabled 디렉토리를 생성해서 관리해주시면 됩니다. (nginx 버전에 따라 생성되어 있을 수도...)

생성한 경로는 nginx.conf 에 include 시키면 됩니다.

 

 

저는 일단 내부용 프로젝트기 때문에 별도의 추가 없이 위 상태에서 작업을 진행합니다.

conf.d 폴더 하위에 default.conf 가 존재할 겁니다.

만약 존재 하지 않는다면 default.conf 내용이 nginx.conf 에 작성 되어있을 수 있습니다.

 

 

server {
    listen       80;
    server_name  sub1.AAA.co.kr;

    include /etc/nginx/conf.d/service-url.inc;

    location / {
        proxy_pass http://127.0.0.1:$service_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
    }
}

server {
    listen          80;
    server_name     jenkins.BBB.co.kr;

    location / {
        proxy_pass http://127.0.0.1:8888;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
    }
}

 

기존 주석처리된 내용 다 지워버리고

server 구문을 위와 같이 2개 작성하고 server_name을 각각 명시해줍니다.

 

저는 sub1.AAA.co.kr 에는 웹어플리케이션을 무중단 배포하기 위해 여러포트로 동작하게끔 해놨고

jenkins.BBB.co.kr 에는 젠킨스접근을 위한 8888포트에 대한 프록시를 설정해놨습니다.

 

nginx를 재기동하면 위 도메인에 따라 각각 접근가능해집니다.

 

systemctl restart nginx

 

 

 

 

* 간혹 내부 로컬 사이트로 접근이 안되고 no resolver defined to resolve (domain) 에러가 날때가 있는데

nginx 에 resolver를 설정해주면 됩니다.

 

nginx.conf http 구문에 resolver 를 추가해줍시다.

 

http {
    resolver 127.0.0.1; 	# resolver 추가
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/default.conf;

}

 

 

 

 

 

반응형
댓글
반응형
최근에 올라온 글
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Total
Today
Yesterday