environment/centos7-linux

nginx 설치 및 tomcat 연동

yoursyun 2020. 12. 9. 14:25

[root@pororicom /]# yum list installed | grep nginx

[root@pororicom /]# vi /etc/yum.repos.d/nginx.repo

 

           #  아래 내용작성 저장소 생성 * nginx 공식사이트에서 확인 할것.

           #  centos 7 기반 - baseurl

           [nginx]

           name=nginx repo

           baseurl=http://nginx.org/packages/centos/7/$basearch/

           gpgcheck=0

           enabled=1

          

[root@pororicom /]# yum install -y nginx                                   - nginx 설치

[root@pororicom /]# systemctl start nginx                                 - nginx 서비스 실행.

[root@pororicom /]# firewall-cmd --permanent --zone=public --add-service=http

[root@pororicom /]# firewall-cmd --permanent --zone=public --add-service=https

[root@pororicom /]# firewall-cmd --reload

 

# 브라우져에서 localhost 테스트 하여 확인한다.

[root@pororicom /]#  systemctl enable nginx            - 자동실행등록.

 

# nginx - tomcat 연동

[root@pororicom /]# vi /etc/nginx/conf.d/nginx.conf

 

upstream tomcat7 {

           ip_hash;

           server 127.0.0.1:8080;     

           # 주의 : docker 에서 사용시, 컨테이너의 아이피를 작성

           #                      server 127.0.0.2:8080; 와 같이 여러개 추가시 자동으로 loadbalance가 적용되며

           #         , 이를 위한 nginx 사용이다. 옵션 weight = 5 로 특정서버에 가중치를 분할 할수 있다.

}

 

server {

    listen       80;

    server_name  localhost;

 

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

 

    location / {

                      

                      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;

 

                      proxy_set_header                    X-Forwarded-Proto $scheme;

                      proxy_set_header                    X-NginX-Proxy    true;               

 

                      proxy_pass                 http://tomcat7; * upstream 의 명칭을 작성

                      proxy_redirect              off;

                      charset                      utf-8;

            }

 

           error_page   500 502 503 504  /50x.html;

           location = /50x.html {

                    root   /usr/share/nginx/html;

           }

}

 

# default.conf 파일 삭제

[root@pororicom /]# rm default.conf

# nginx reload

[root@pororicom /]# nginx -s reload

# nginx proxy 접근시 접근권한을 풀어준다.

[root@pororicom /]# setsebool -P httpd_can_network_connect true

반응형