Server/기타

나만의 Nginx setting

삼군개발자 2023. 8. 31. 19:05

설치

$ sudo yum install yum-utils
$ sudo vi /etc/yum.repos.d/nginx.repo

아래 내용 입력
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

$ sudo yum info nginx
$ sudo yum install nginx
$ nginx -v
nginx version: nginx/1.22.1

$ sudo systemctl start nginx
$ sudo systemctl status nginx

Proxy 설정

DMZ 구간에 프록시 서버를 배치하여 개발 환경을 외부(모바일)에서 접속할 수 있도록 제공

conf 파일 경로 : /etc/nginx/nginx.conf

nginx.conf

user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    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_${logdate}.log;  main;
 
    map $time_iso8601 $logdate {
        '~^(?<ymd>\d{4}-\d{2}-\d{2})'   $ymd;
        default                         'nodate';
    }
  
    #block bot's crawling
    map $http_user_agent $limit_bots {
        default 0;
        ~*(google|bing|yandex|msnbot) 1;
        ~*(AltaVista|Googlebot|Slurp|BlackWidow|Bot|ChinaClaw|Custo|DISCo|Download|Demon|eCatch|EirGrabber|EmailSiphon|EmailWolf|SuperHTTP|Surfbot|WebWhacker) 1;
        ~*(Express|WebPictures|ExtractorPro|EyeNetIE|FlashGet|GetRight|GetWeb!|Go!Zilla|Go-Ahead-Got-It|GrabNet|Grafula|HMView|Go!Zilla|Go-Ahead-Got-It) 1;
        ~*(rafula|HMView|HTTrack|Stripper|Sucker|Indy|InterGET|Ninja|JetCar|Spider|larbin|LeechFTP|Downloader|tool|Navroad|NearSite|NetAnts|tAkeOut|WWWOFFLE) 1;
        ~*(GrabNet|NetSpider|Vampire|NetZIP|Octopus|Offline|PageGrabber|Foto|pavuk|pcBrowser|RealDownload|ReGet|SiteSnagger|SmartDownload|SuperBot|WebSpider) 1;
        ~*(Teleport|VoidEYE|Collector|WebAuto|WebCopier|WebFetch|WebGo|WebLeacher|WebReaper|WebSauger|eXtractor|Quester|WebStripper|WebZIP|Wget|Widow|Zeus) 1;
        ~*(Twengabot|PetalBot|htmlparser|libwww|Python|perl|urllib|scan|email|PycURL|Pyth|PyQ|WebCollector|WebCopy|webcraw) 1;
    }
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    #gzip  on;
    server_names_hash_bucket_size 512;
  
    proxy_cache_path cache levels=1:2 keys_zone=webcache:64m max_size=1024m inactive=60m;
    proxy_cache_key "$scheme$proxy_host$request_uri";
    proxy_cache_methods GET;
    #proxy_cache_methods GET POST;
 
    #gzip  on;
 
    include /etc/nginx/conf.d/*.conf;
}

 

 

 

웹 서버 설정

설정된 DNS 주소로 접근하는 요청에 대해서 nginx 가 프록시 서버 역할을 해서 root 경로에 대해서는 reactjs 빌드 파일을 /api 경로에 대해서는 was 로 연결 되도록 설정

 

quiz.conf

upstream api_servers {
    server 000.000.000.000:8080; #api를 보낼 was server의 ip
}
 
server {
    listen       80;
    server_name  웹서버의 도메인(api_servers아님);   
 
    #charset koi8-r;
 
    #access_log  logs/host.access.log  main;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Queue-Start t=${msec}000;
 
    location / {
        root /home/ec2-user/deploy; # 배포된 웹 파일 경로
        index index.html index.htm; # 웹 파일명
        try_files $uri $uri/ /index.html;
    }
 
    location /api {
        #rewrite ^/api(.*)$ $1?$args break;
        proxy_pass http://api_servers; # upstream 주소
        proxy_redirect off;
    }
 
    location /api/ws {
        proxy_pass http://api_servers; # upstream 주소
 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
    }
 
 
    location /api/images {
        proxy_pass http://api_servers; # upstream 주소
        proxy_cache webcache;
        proxy_cache_valid 10m;
        proxy_redirect off;
 
        add_header X-Proxy-Cache $upstream_cache_status;
    }
}
 
client_max_body_size 10M;