Nginx

main config

/opt/nginx/conf/nginx.conf

user  nobody nobody;
worker_processes  auto;
worker_cpu_affinity auto;
pid        logs/nginx.pid;
error_log  logs/error.log;
include /opt/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 655350;

events {
    worker_connections  102400;
	# multi_accept on;
}

##
# TCP Stream Settings
##
stream{
    log_format tcp_log '$remote_addr [$time_local]'
         '$protocol $status $bytes_sent $bytes_received $session_time'
         '"$upstream_addr" "$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
    access_log /opt/nginx/logs/tcp-access.log tcp_log;

    upstream tcp_backend_server {
        hash $remote_addr consistent; #IP hash
        server 1.1.1.1:9999;
        server 2.2.2.2:9999;
        server 3.3.3.3:9999;
    }
    server {
      listen 9999;
      proxy_pass tcp_backend_server;
    }
}

http {
    ###
    # Basic Settings
    ##
	
    sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;
    server_tokens off;
    server_names_hash_max_size 3072;
    server_names_hash_bucket_size 1024;
	
    include       mime.types;
    default_type  application/octet-stream;
	
    ##
    # SSL Settings
    ##
    #ssl_protocols SSLv3 TLSv1.0 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    ## 
    # Logging Settings
    ##
    log_format main '$remote_addr $remote_user $ip2location_country_long $ip2location_region $ip2location_city '
                    '[$time_local] Host:$host Request:$request Status:$status RequestLength:$request_length BodyBytesSent:$body_bytes_sent RequestTime:$request_time '
                    'UpstreamAddr:$upstream_addr UpstreamStatus:$upstream_status UpstreamConnTime:$upstream_connect_time UpstreamResTime:$upstream_response_time '
                    'Scheme:$scheme Referer:$http_referer Cookie:$http_cookie UA:$http_user_agent XFF:$http_x_forwarded_for'
    log_format main_json escape=json '{"time_local":"$time_local",'
'"server_addr":"$server_addr",'
'"country":"$ip2location_country_long",'
'"state":"$ip2location_region",'
'"city":"$ip2location_city",'
'"http_x_forward":"$http_x_forwarded_for",'
'"remote_addr":"$remote_addr",'
'"request_method":"$request_method",'
'"uri":"$uri",'
'"scheme":"$scheme",'
'"domain":"$server_name",'
'"referer":"$http_referer",'
'"server_name":"$host",'
'"request":"$request_uri",'
'"http_user_agent":"$http_user_agent",'
'"args":"$args",'
'"body":"$request_body",'
'"cookie":"$http_cookie",'
'"request_length":"$request_length",'
'"size":$body_bytes_sent,'
'"request_completion":"$request_completion",'
'"status": "$status",'
'"proxy_host":"$proxy_host",'
'"response_time":$request_time,'
'"upstream_time":"$upstream_response_time",'
'"upstream_status":"$upstream_status",'
'"upstream_addr":"$upstream_addr",'
'"upstream_cache_status":"$upstream_cache_status",'
'"upstream_connect_time":"$upstream_connect_time",'
'"upstream_response_length":"$upstream_response_length",'
'"https":"$https",'
'"request_id":"$hostname-$request_id"'
'}';
    access_log /opt/nginx/logs/access.log main;

    ##
    # Gzip Settings
    ##
    gzip  on;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1k;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/octet-stream;

	
    resolver 8.8.8.8 1.1.1.1 114.114.114.114 valid=5 ipv6=off;
    resolver_timeout 3s;

    keepalive_timeout 180;
    client_body_buffer_size 50m;
    client_body_timeout 300;
    client_max_body_size 50m;
    proxy_intercept_errors on;
    proxy_ignore_client_abort on;
    proxy_next_upstream error timeout http_502 http_503 http_504;
    proxy_next_upstream_timeout 20;
    proxy_connect_timeout 10;
    proxy_read_timeout 300;
    proxy_send_timeout 300;
    proxy_buffer_size 512k;
    proxy_buffers 4 512k;
    proxy_busy_buffers_size 512k;
    proxy_temp_file_write_size 512k;
    client_header_buffer_size 512k;
    large_client_header_buffers 4 512k;

    variables_hash_max_size 2048;
    variables_hash_bucket_size 2048;


    ##
    # Map Settings
    ##
    # ip2location = https://github.com/chrislim2888/IP2Location-C-Library
    ip2location_database /opt/nginx/conf/IPV6-COUNTRY-REGION-CITY.BIN;
    ip2location_proxy_recursive on;
    map $ip2location_country_short $blocked_country {
	    default no;
	    ~*(AU|IN|NG|US)$ yes;
    }
    ## geoip2 = https://github.com/leev/ngx_http_geoip2_module
    #geoip2 /opt/nginx/conf/GeoLite2-Country.mmdb {
    #   $geoip2_country_code country iso_code;
    #   $geoip2_country_name country names en;
    #}
    #geoip2 /opt/nginx/conf/GeoLite2-City.mmdb {
    #    $geoip2_city_name city names en;
    #    $geoip2_subdivisions_name subdivisions 0 names en;
    #    $geoip2_latitude location latitude;
    #    $geoip2_longitude location longitude;
    #}
    #map $geoip2_country_code $allowed_country {
    #    default no;
    #    ~*(AU|IN|NG|US)$ yes;
    #}

    # websocket connection keepalive
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    # domain ssl dir
    map $ssl_server_name $domainCert {
       default /opt/nginx/conf/keys/default.crt;
       ~*^(.+\.)*([^\.]+\.[^\.]+)$ /opt/nginx/conf/keys/$2.crt;
    }
    map $ssl_server_name $domainKey {
       default /opt/nginx/conf/keys/default.key;
       ~*^(.+\.)*([^\.]+\.[^\.]+)$ /opt/nginx/conf/keys/$2.key;
    }

    ##
    # Virtual Host Configs
    ##
    include vhosts/*.conf;
}


include modules.conf;

modules.conf

third iplib

[[IPV6-COUNTRY-REGION-CITY.BIN.gz|ip2location]]

[[GeoLite2-City.mmdb.gz|Geoip2]]

virtual host conf

/opt/nginx/conf/vhosts/default.conf

/opt/nginx/conf/vhosts/real_ip.conf

/opt/nginx/conf/vhosts/template.conf

Last updated