Nginx Config

nginx.conf
user nginx;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 100000;

events {
    worker_connections 4000;
    use epoll;
    multi_accept on;
}

http {
    # DDoS Defense
    limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
    limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s;
    limit_conn_status 429;
    limit_req_status 429;

    client_body_buffer_size  128k;
    client_header_buffer_size 3m;
    large_client_header_buffers 4 256k;
    client_body_timeout   5;
    client_header_timeout 5;

    reset_timedout_connection on;
    send_timeout 5;
    keepalive_timeout 5 5;
    keepalive_requests 100000;

    charset utf-8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    log_not_found off;
    types_hash_max_size 2048;
    client_max_body_size 100M;

    # MIME
    include mime.types;
    default_type application/octet-stream;

    # logging
    access_log off;
    error_log /var/log/nginx/error.log warn;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
}
default.conf
server {
    listen      80;
    server_name "";
    return      444;
}
general.conf
# security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

# . files
location ~ /\.(?!well-known) {
    deny all;
}
nginx/sites-available/your_domain_name.conf
server {
    listen 80;
    server_name YOUR_DOMAIN_NAME;
    root /var/www/html/laravel/public;

    if ($http_x_forwarded_proto = "http") {
        return 301 https://$server_name$request_uri;
    }

    index index.html index.htm index.php;

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

    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
        limit_conn conn_limit_per_ip 10;
        limit_req zone=req_limit_per_ip burst=10 nodelay;
    }

    include general.conf;
}

解決 laradock pathinfo PATHINFO_BASENAME 不支持中文開頭的檔名

  • 先透過 locale -a 查詢作業系統支援那些語系編碼
  • 若未安裝額外語系編碼,則至 laradock .env 設置
    • PHP_FPM_INSTALL_ADDITIONAL_LOCALES=true
    • PHP_FPM_ADDITIONAL_LOCALES=”zh_TW.UTF-8″
  • 重新編譯 PHP-FPM:docker-compose build php-fpm
  • setlocale(LC_ALL,’zh_TW.utf8′);

Javascript 幾種 Array 填充 0 的方式

# Array push
for (var i = 0, a = []; i < 100; i++) a.push(0);

# Array assign
for (var i = 0, a = new Array(100); i < 100;) a[i++] = 0;

# Typed array conversion (最慢)
[].slice.call(new Uint8Array(100));

# Binary concatenation
for (var i = 100, a = [], add = [0]; i; i >>=1) {
    if (i & 1) a.push.apply(a, add);
    add.push.apply(add, add);
}

# Array Apply
Array.apply(null, new Array(100)).map(Number.prototype.valueOf,0);

# Array.fill() (最快)
new Array(100).fill(0);

Most efficient way to create a zero filled JavaScript array? – Stack
Overflow Zero filled array creation

Responsive iFrames with jQuery

HTML
iframe {
    max-width: 100%;
}
Javascript
function adjustIframes() {
    $('iframe').each(function() {
        var
            $this = $(this),
            proportion = $this.data('proportion'),
            w = $this.attr('width'),
            actual_w = $this.width();

        if (!proportion) {
            proportion = $this.attr('height') / w;
            $this.data('proportion', proportion);
        }

        if (actual_w != w) {
            $this.css('height', Math.round(actual_w * proportion) + 'px');
        }
    });
}

$(window).on('resize load', adjustIframes);

MySQL、MariaDB 資料庫匯入與匯出

匯出資料庫

mysqldump -u username -p database_name > data-dump.sql
  • username 資料庫的使用者名稱
  • database_name 要匯出的資料庫名稱
  • data-dump.sql 要匯出的 sql 檔案名稱

匯入資料庫

mysql -h hostname -u username -ppassword --default-character-set=utf8 database_name < data-import.sql
  • hostname 伺服器名稱或 IP 位址
  • username 資料庫的使用者名稱
  • password 資料庫的使用者密碼(注意,密碼和 -p 之間沒有空格)
  • database_name 要匯入的資料庫名稱
  • data-import.sql 要匯入的 sql 檔案名稱

LAMP on CentOS 7

Apache

# Step 1: Install Apache
yum clean all
yum -y update
yum -y install httpd

# Step 2: Allow Apache Through the Firewall
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
firewall-cmd --reload

# Step 3: Configure Apache to Start on Boot
systemctl start httpd
systemctl enable httpd

PHP 7

# CentOS/RHEL 7.x
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

# Install php 7
yum -y install php70w php70w-opcache

MySQL / MariaDB

# Install MySQL / MariaDB
yum -y install mariadb-server mariadb
systemctl start mariadb
systemctl enable mariadb

phpMyAdmin

# Step 1: Add the EPEL Repository
rpm -iUvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

# Step 2: Install phpMyAdmin
yum -y update
yum -y install phpmyadmin

# Step 3: Basic Configuration for phpMyAdmin
vi /etc/httpd/conf.d/phpMyAdmin.conf
systemctl restart httpd

SELinux

# Disabled SELINUX
vi /etc/sysconfig/selinux
set SELINUX=disabled