Hướng dẫn config nginx php-fpm

  • March 18, 2023
  • 344

NGINX là gì?

Nginx là một web server mã nguồn mở có hiệu suất cao và ổn định, hỗ trợ reverse proxy, caching, load balancer, media streaming...
Nginx là gì

Cài đặt nginx trên EC2 linux

Cài đặt nginx trên ec2 máy chủ linux


// Update yum
sudo yum update

// Install Nginx
sudo amazon-linux-extras list | grep nginx
sudo amazon-linux-extras enable nginx1
sudo yum -y install nginx

// Check config
nginx -t

// Start nginx
sudo systemctl start nginx

// Stop nginx
sudo systemctl stop nginx

// Restart nginx
sudo systemctl restart nginx

// Check status nginx
sudo systemctl status nginx

Config Nginx với PHP-FPM

Khai báo một file cấu hình nginx với php-fpm


server {
    listen 80;
    server_name  _;
    root         /var/www/html/app/public;
    
    # Redirect to https
    #if ($host = mysite.net) {
        #return 301 https://$host$request_uri;
    #}

    #listen 80;
    #server_name mysite.net www.mysite.net;
    #return 301 https://mysite.net$request_uri;

    charset utf-8;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log warn;

    #Disable public server informational
    server_tokens off;
    
    ## favicon
    location ~* favicon.ico$ {
        access_log        off;
        expires           max;
    }

    # Images and static content is treated different
    location ~* ^/(img|css|js|font).+\.(jpg|jpeg|gif|css|png|js|ico|xml|ttf|woff.|eot|svg|map)$ {
      access_log        off;
      expires           1800;
    }

    location /.well-known/assetlinks.json {
        set $auth "off";
    }

    # Set off basic auth
    set $auth "Administrator’s Area";
    if ($request_uri ~ ^/api/.*){
        set $auth "off";
    }

    location / {
        # Config basic auth
        auth_basic $auth;
        auth_basic_user_file /etc/nginx/.htpasswd;

        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;  
        include fastcgi_params;
        # use sock
        fastcgi_pass unix:/run/php-fpm/www.sock;
        # common cgi
        # fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
   
   # Gzip
   gzip on;
   gzip_min_length 2048;
   gzip_types text/plain
            text/css
            text/xml
            text/javascript
            application/json
            application/javascript
            application/xml
            application/xhtml+xml
            application/rss+xml
            application/x-javascript
            application/atom+xml
            application/x-httpd-php;
   gzip_buffers 4 8k;
   gzip_disable "msie6";
   gzip_proxied any;
   gzip_http_version 1.0;
   gzip_vary on;
   gzip_comp_level 6;

   add_header X-Frame-Options SAMEORIGIN;
   
   # Config maxsize upload
   client_max_body_size 30M;

   fastcgi_read_timeout 180;
   proxy_read_timeout 180;

    # Deny access to . files, for security
    location ~ /\. {
        log_not_found off;
        deny all;
    }
}

Dựa vào file cấu hình trên giúp bạn có thể thiết lập maxsize upload, gzip file, cache file, cấm truy cập đến 1 file, setting basic auth, chuyển hướng trang... File .htpasswd chứa user và pass của basic auth, tạo htpasswd online.


# Tạo basic auth
# User: test, pass: 5fx/FG1a
# Content file htpasswd là
test:$apr1$SE7FlqCL$SyJtfc2QxT18MLFycwguh1

Thêm access log và error log để check hoạt động của nginx với php-fpm


tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log 

Tổng kết

Như chúng ta đã cấu hình được một web server mạnh mẽ rồi đó, các bạn còn sử dụng cấu hình nào khác hãy comment nhé. Thanks for reading...