Nginx 상용 명령 및 설정

nginx 설치
1. 로 컬 설치
  • windows 시스템:
  • 직접 홈 페이지 로 가기:https://nginx.org/en/download... 해당 버 전 을 다운로드 하면 됩 니 다.
  • mac 시스템:
  • $ brew install nginx

    2. Linux 설치:
    centos 시스템 의 경우 다음 두 가지 설치 방식 이 있 습 니 다 (추천 1)
    1. rpm 미 러 소스 를 통 해 설치
    $ rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    $ yum install -y nginx

    2.) 패키지 에 의존 하여 자세히 설치
    nginx 의존 라 이브 러 리 pcre, zlib 설치
    $  yum install pcre pcre-devel  
    $  yum install zlib zlib-devel

    필요 하 다 면 c + 컴 파일 환경 과 openssl 을 설치 할 수 있 습 니 다.
    $  yum install gcc-c++
    $  yum install openssl openssl-devel

    다운로드 / 컴 파일 nginx
    $ wget -c https://nginx.org/download/nginx-1.16.0.tar.gz
    $ tar -zxvf nginx-1.16.0.tar.gz
    
    #     
    $ cd nginx-1.16.0
    $ ./configure  #      /usr/local/nginx 
    $ make && make install
    
    #     
    $ ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
    $ nginx -v

    nginx 명령
    # windows  
    > start nginx
    
    # linux/mac  
    $ service nginx start
    
    #       
    $ nginx -c /usr/local/nginx/conf/nginx.conf
    
    # -p  nginx    (      )
    $ nginx -c /path/nginx.conf -p /path/
    
    #   
    $ nginx -s reload
    
    #   
    $ nginx -s stop
    
    #     
    $ netstat -an | grep     # linux/mac  
    > netstat -an | findstr     # windows  
    
    #   web  
    $ curl -i   :  
    #  
    $ telnet      
    
    #     
    $ ps -ef | grep nginx
    
    #       
    $ tail -30 /var/log/nginx/error.log

    nginx 설정
    nginx. conf 설정 파일 위치 보기
    $ nginx -t

    1. 표준 서버 만 들 기
    nginx. conf 에 include conf. d / *. conf 가 활성화 되 었 는 지 확인 하 십시오. 없 으 면 하 나 를 추가 합 니 다.
    conf. d 디 렉 터 리 에 server. conf 파일 을 새로 만 듭 니 다. 설정 은 다음 과 같 습 니 다.
    server {
        listen       80;
        server_name  127.0.0.1;
    
        client_max_body_size    100m;
        
        location / {
            root   /app/xxx;  #       
            index  index.html index.htm;
            try_files  $uri $uri/ /index.html;  # vue            index.html
        }
    }

    2. ssl 인증 서 를 설정 하여 https 접근 을 실현 합 니 다.
    현재 server 설정 과 같은 디 렉 터 리 에. pem 과. key 두 인증 서 를 복사 합 니 다.
    server {
        listen       443;
        server_name  127.0.0.1;
        ssl on;
        ssl_certificate             my.pem;    #         
        ssl_certificate_key         my.key;    #         
        ssl_session_timeout         5m;
        ssl_protocols SSLv3 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
        ssl_prefer_server_ciphers   on;
    
        location / {
            ...
        }
    }

    3. api 인터페이스 역방향 에이전트
        location /api {
            proxy_pass   http://b.domain.com:9000;  #        /api,   /api/xxx
            #proxy_cookie_domain   b.domain.com a.domain.com; #          cookie     
        }

    주의해 야 할 것 은, proxypass 경 로 는 상대 적 이 고 절대적 인 구분 이 있 습 니 다. 예 를 들 어 proxypass http://b.domain.com:9000/; # 최종 주 소 는 / api 를 대체 하여 / xxx 로 변 합 니 다.
    4. upstream 부하 균형
    upstream apiServer { 
        server 10.0.0.80:5000;  #         weight=  
        server 10.0.0.81:5000;
    }
    server {
        listen       80;
        server_name  127.0.0.1;
    
        location /api {
            proxy_pass   http://apiServer;
        }
    }

    주의해 야 할 것 은:
    upstream 이름 은 밑줄 을 포함 해 서 는 안 됩 니 다. 어떤 조건 에서 호스트 이름 으로 백 엔 드 자바 애플 리 케 이 션 에 전달 하면 도 메 인 이름 으로 해석 되 고 결 과 는 Null 로 돌아 가 서버 내부 오류 가 발생 하기 쉽 기 때 문 입 니 다.제안:
    낙타 봉 명명 규범 사용
    5. 역 간 접근 허용
    location / api {} 에 다음 항목 을 추가 합 니 다:
        add_header Access-Control-Allow-Origin *;  # *           (   ,            :http://b.domain.com:9000(    :http(s):// + domain + port,      /)
        add_header Access-Control-Allow-Credentials true;  #      cookie    ,   true,         *,        

    6. iframe 동원 정책 제한
    nginx 설정 을 통 해 iframe 을 통 해 이 사이트 에 접근 하 는 것 을 제한 할 수 있 습 니 다. 기본 값 은 접근 할 수 있 습 니 다.
    #        
    add_header X-Frame-Options SAMEORIGIN
    
    #       
    add_header X-Frame-Options "ALLOW-FROM http://xxx.com:8000";
    add_header Content-Security-Policy "frame-ancestors http://xxx.com:8000"; #   chrome

    7 、 gzip 압축 열기
    gzip 필드 를 설정 하고 압축 할 파일 형식 을 설정 합 니 다.
    http {
        include          mime.conf;
        default_type     application/octet-stream;
        ....
    
        gzip             on;
        gzip_min_length  10k;
        gzip_comp_level  5;
        gzip_types       text/plain text/css application/x-javascript application/javascript text/javascript;
    
        server {
            ....
        }

    8. 기타 문제
    1.) 액세스 서비스 보고서 403 권한
    nginx. conf 의 user 를 수정 해 야 합 니 다. 예 를 들 어 user root;
    2. nginx 재 부팅 타 임 스 pid 오류:
    nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
    해결 방안: nginx - c 의 매개 변 수 를 사용 하여 nginx. conf 파일 의 위 치 를 지정 합 니 다.
    $ mkdir -p /usr/local/nginx/logs
    $ touch /usr/local/nginx/logs/nginx.pid
    $ nginx -c /usr/local/nginx/conf/nginx.conf

    좋은 웹페이지 즐겨찾기