42 SEOUL/배운 것들 정리

[42Seoul/ft_server] nginx 설정파일 (autoindex / ssl / redirect 설정)

hi._.0seon 2021. 2. 16. 16:04
반응형
server {
        listen 80;
        listen [::]:80;

        return 301 https://$host$request_uri;
}
  • listen

    nginx에게 http 연결을 위해 필요한 hostname/IP 와 TCP 포트를 알려준다
  • listen 80

    http 연결을 위한 80번 포트에 대해 동작

    IPv4 HTTP 패킷을 리다이렉션
  • listen [::]:80

    모든 IPv6 HTTP 트래픽에 대해 작동
  • return 301 https://$host$request_uri;

    - 301 : HTTP 응답 상태 코드. 영구적인 URL 리다이렉션을 위해 사용됨

    - $host$request_uri : domain 주소 + 클라이언트가 요청한 url
server {
        # SSL configuration
        listen 443 ssl;
        listen [::]:443 ssl;

        # ssl setting
        ssl on;
        ssl_certificate /etc/ssl/certs/localhost.dev.crt;
        ssl_certificate_key /etc/ssl/private/localhost.dev.key;
        
         # server root directory config
        root /var/www/html;

        # Add index.php to the list if you are using PHP
        # Auto index
        index index.html index.htm index.nginx-debian.html index.php;

        server_name ft_server;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                autoindex on;
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        }
}
  • ssl을 사용한다고 listening sockets에 파라미터로 적어둔다
  • ssl 인증서와 비공개키의 위치를 적어둔다.
  • location 블록
    서버 안의 리소스에 대한 요청을 어떻게 응답해야 할지를 설정
    특정 파일과 특정 디렉토리에 대한 요청을 처리함
  • location ~ \.php$

    ~ : 정규 표현식 매치를 수행
    .php 로 끝나는 모든 요청들은 이 블럭에 의해 처리됨
  • include snippets/fastcgi-php.conf;
    fastcgi-pass unix:/run/php/php7.3-fpm.sock;
  • autoindex

    루트 디렉토리에 있는 파일들을 자동으로 연결해 주는 것
  • try_files $uri $uri/ =404;
    root 폴더 내에 uri에 따른 폴더가 있는지 찾아보고 없으면 404 에러
  • server_name 은 방문자가 들어온 주소에 따라 해당 도메인 이름을 가진 server 블록이 요청을 처리함

fastcgi_pass 는 php-fpm 과 Nginx 를 연결하기 위한 인터페이스를 지정하는 것

php-fpm의 listen 설정값과 일치해야 한다.

 

include snippets/fastcgi-php.conf;

fastcgi_pass 

-> php 스크립트를 FastCGI 서버로 보내는 것

보내기 위한 설정파일을 include 하고,

소켓 경로를 적어둔다.

 

 

 

architectophile.tistory.com/12

 

[Nginx] 기본 설정 방법

[Nginx] 기본 설정 방법 Nginx는 가벼운 고성능의 웹서버로서 높은 트래픽 처리를 위해 디자인되었다. Nginx의 가장 강력한 기능 중 하나는 HTML이나 미디어 파일 같은 정적 컨텐츠를 효율적으로 서브

architectophile.tistory.com

juneyr.dev/nginx-basics

 

나는 nginx 설정이 정말 싫다구요

nginx 설정 nginx.conf와 conf.d를 알아보자

juneyr.dev

 

반응형