Advertisement
chinglin

nginx-intgress-config

Aug 13th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nginx 17.56 KB | None | 0 0
  1. $ 8exec zbms6 cat /etc/nginx/nginx.conf
  2.  
  3. # Configuration checksum: 16399570357895000238
  4.  
  5. # setup custom paths that do not require root access
  6. pid /tmp/nginx.pid;
  7.  
  8. daemon off;
  9.  
  10. worker_processes 40;
  11.  
  12. worker_rlimit_nofile 25190;
  13.  
  14. worker_shutdown_timeout 10s ;
  15.  
  16. events {
  17.     multi_accept        on;
  18.     worker_connections  16384;
  19.     use                 epoll;
  20. }
  21.  
  22. http {
  23.     lua_package_path    "/usr/local/openresty/site/lualib/?.ljbc;/usr/local/openresty/site/lualib/?/init.ljbc;/usr/local/openresty/lualib/?.ljbc;/usr/local/openresty/lualib/?/init.ljbc;/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua;./?.lua;/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/openresty/luajit/share/lua/5.1/?.lua;/usr/local/openresty/luajit/share/lua/5.1/?/init.lua;/usr/local/lib/lua/?.lua;;";
  24.     lua_package_cpath   "/usr/local/openresty/site/lualib/?.so;/usr/local/openresty/lualib/?.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so;;";
  25.    
  26.     lua_shared_dict configuration_data 15M;
  27.     lua_shared_dict certificate_data 16M;
  28.    
  29.     init_by_lua_block {
  30.         collectgarbage("collect")
  31.        
  32.         local lua_resty_waf = require("resty.waf")
  33.         lua_resty_waf.init()
  34.        
  35.         -- init modules
  36.         local ok, res
  37.        
  38.         ok, res = pcall(require, "lua_ingress")
  39.         if not ok then
  40.         error("require failed: " .. tostring(res))
  41.         else
  42.         lua_ingress = res
  43.         lua_ingress.set_config({
  44.             use_forwarded_headers = false,
  45.             is_ssl_passthrough_enabled = false,
  46.             http_redirect_code = 308,
  47.         listen_ports = { ssl_proxy = "442", https = "443" },
  48.         })
  49.         end
  50.        
  51.         ok, res = pcall(require, "configuration")
  52.         if not ok then
  53.         error("require failed: " .. tostring(res))
  54.         else
  55.         configuration = res
  56.     configuration.nameservers = { "180.76.76.76", "172.31.90.17" }
  57.         end
  58.        
  59.         ok, res = pcall(require, "balancer")
  60.         if not ok then
  61.         error("require failed: " .. tostring(res))
  62.         else
  63.         balancer = res
  64.         end
  65.        
  66.         ok, res = pcall(require, "monitor")
  67.         if not ok then
  68.         error("require failed: " .. tostring(res))
  69.         else
  70.         monitor = res
  71.         end
  72.        
  73.         ok, res = pcall(require, "certificate")
  74.         if not ok then
  75.         error("require failed: " .. tostring(res))
  76.         else
  77.         certificate = res
  78.         end
  79.        
  80.         ok, res = pcall(require, "plugins")
  81.         if not ok then
  82.         error("require failed: " .. tostring(res))
  83.         else
  84.         plugins = res
  85.         end
  86.         -- load all plugins that'll be used here
  87.     plugins.init({})
  88.     }
  89.    
  90.     init_worker_by_lua_block {
  91.         lua_ingress.init_worker()
  92.         balancer.init_worker()
  93.        
  94.         monitor.init_worker()
  95.        
  96.         plugins.run()
  97.     }
  98.    
  99.     geoip_country       /etc/nginx/geoip/GeoIP.dat;
  100.     geoip_city          /etc/nginx/geoip/GeoLiteCity.dat;
  101.     geoip_org           /etc/nginx/geoip/GeoIPASNum.dat;
  102.     geoip_proxy_recursive on;
  103.    
  104.     aio                 threads;
  105.     aio_write           on;
  106.    
  107.     tcp_nopush          on;
  108.     tcp_nodelay         on;
  109.    
  110.     log_subrequest      on;
  111.    
  112.     reset_timedout_connection on;
  113.    
  114.     keepalive_timeout  75s;
  115.     keepalive_requests 100;
  116.    
  117.     client_body_temp_path           /tmp/client-body;
  118.     fastcgi_temp_path               /tmp/fastcgi-temp;
  119.     proxy_temp_path                 /tmp/proxy-temp;
  120.     ajp_temp_path                   /tmp/ajp-temp;
  121.    
  122.     client_header_buffer_size       1k;
  123.     client_header_timeout           60s;
  124.     large_client_header_buffers     4 8k;
  125.     client_body_buffer_size         8k;
  126.     client_body_timeout             60s;
  127.    
  128.     http2_max_field_size            4k;
  129.     http2_max_header_size           16k;
  130.     http2_max_requests              1000;
  131.    
  132.     types_hash_max_size             2048;
  133.     server_names_hash_max_size      1024;
  134.     server_names_hash_bucket_size   32;
  135.     map_hash_bucket_size            64;
  136.    
  137.     proxy_headers_hash_max_size     512;
  138.     proxy_headers_hash_bucket_size  64;
  139.    
  140.     variables_hash_bucket_size      128;
  141.     variables_hash_max_size         2048;
  142.    
  143.     underscores_in_headers          off;
  144.     ignore_invalid_headers          on;
  145.    
  146.     limit_req_status                503;
  147.     limit_conn_status               503;
  148.    
  149.     include /etc/nginx/mime.types;
  150.     default_type text/html;
  151.    
  152.     gzip on;
  153.     gzip_comp_level 5;
  154.     gzip_http_version 1.1;
  155.     gzip_min_length 256;
  156.     gzip_types application/atom+xml application/javascript application/x-javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/javascript text/plain text/x-component;
  157.     gzip_proxied any;
  158.     gzip_vary on;
  159.    
  160.     # Custom headers for response
  161.    
  162.     server_tokens on;
  163.    
  164.     # disable warnings
  165.     uninitialized_variable_warn off;
  166.    
  167.     # Additional available variables:
  168.     # $namespace
  169.     # $ingress_name
  170.     # $service_name
  171.     # $service_port
  172.     log_format upstreaminfo '$the_real_ip - [$the_real_ip] - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id';
  173.    
  174.     map $request_uri $loggable {
  175.        
  176.         default 1;
  177.     }
  178.    
  179.     access_log /var/log/nginx/access.log upstreaminfo  if=$loggable;
  180.    
  181.     error_log  /var/log/nginx/error.log notice;
  182.    
  183.     resolver 180.76.76.76 172.31.90.17 valid=30s;
  184.    
  185.     # See https://www.nginx.com/blog/websocket-nginx
  186.     map $http_upgrade $connection_upgrade {
  187.         default          upgrade;
  188.        
  189.         # See http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
  190.         ''               '';
  191.        
  192.     }
  193.    
  194.     # The following is a sneaky way to do "set $the_real_ip $remote_addr"
  195.     # Needed because using set is not allowed outside server blocks.
  196.     map '' $the_real_ip {
  197.        
  198.         default          $remote_addr;
  199.        
  200.     }
  201.    
  202.     # Reverse proxies can detect if a client provides a X-Request-ID header, and pass it on to the backend server.
  203.     # If no such header is provided, it can provide a random value.
  204.     map $http_x_request_id $req_id {
  205.         default   $http_x_request_id;
  206.        
  207.         ""        $request_id;
  208.        
  209.     }
  210.    
  211.     # Create a variable that contains the literal $ character.
  212.     # This works because the geo module will not resolve variables.
  213.     geo $literal_dollar {
  214.         default "$";
  215.     }
  216.    
  217.     server_name_in_redirect off;
  218.     port_in_redirect        off;
  219.    
  220.     ssl_protocols TLSv1.2;
  221.    
  222.     # turn on session caching to drastically improve performance
  223.    
  224.     ssl_session_cache builtin:1000 shared:SSL:10m;
  225.     ssl_session_timeout 10m;
  226.    
  227.     # allow configuring ssl session tickets
  228.     ssl_session_tickets on;
  229.    
  230.     # slightly reduce the time-to-first-byte
  231.     ssl_buffer_size 4k;
  232.    
  233.     # allow configuring custom ssl ciphers
  234.     ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
  235.     ssl_prefer_server_ciphers on;
  236.    
  237.     ssl_ecdh_curve auto;
  238.    
  239.     proxy_ssl_session_reuse on;
  240.    
  241.     upstream upstream_balancer {
  242.         server 0.0.0.1; # placeholder
  243.        
  244.         balancer_by_lua_block {
  245.             balancer.balance()
  246.         }
  247.        
  248.         keepalive 32;
  249.        
  250.         keepalive_timeout  60s;
  251.         keepalive_requests 100;
  252.        
  253.     }
  254.    
  255.     # Global filters
  256.    
  257.     ## start server _
  258.     server {
  259.         server_name _ ;
  260.        
  261.         listen 80 default_server reuseport backlog=32768;
  262.        
  263.         listen [::]:80 default_server reuseport backlog=32768;
  264.        
  265.         set $proxy_upstream_name "-";
  266.         set $pass_access_scheme $scheme;
  267.         set $pass_server_port $server_port;
  268.         set $best_http_host $http_host;
  269.         set $pass_port $pass_server_port;
  270.        
  271.         listen 443  default_server reuseport backlog=32768 ssl http2;
  272.        
  273.         listen [::]:443  default_server reuseport backlog=32768 ssl http2;
  274.        
  275.         # PEM sha: 468312431f9e5fa991282a5dcab937301b6c9e65
  276.         ssl_certificate                         /etc/ingress-controller/ssl/default-fake-certificate.pem;
  277.         ssl_certificate_key                     /etc/ingress-controller/ssl/default-fake-certificate.pem;
  278.        
  279.         ssl_certificate_by_lua_block {
  280.             certificate.call()
  281.         }
  282.        
  283.         location / {
  284.            
  285.             set $namespace      "";
  286.             set $ingress_name   "";
  287.             set $service_name   "";
  288.             set $service_port   "0";
  289.             set $location_path  "/";
  290.            
  291.             rewrite_by_lua_block {
  292.                 lua_ingress.rewrite({
  293.                     force_ssl_redirect = false,
  294.                     use_port_in_redirects = false,
  295.                 })
  296.                 balancer.rewrite()
  297.                 plugins.run()
  298.             }
  299.            
  300.             header_filter_by_lua_block {
  301.                
  302.                 plugins.run()
  303.             }
  304.             body_filter_by_lua_block {
  305.                
  306.             }
  307.            
  308.             log_by_lua_block {
  309.                
  310.                 balancer.log()
  311.                
  312.                 monitor.call()
  313.                
  314.                 plugins.run()
  315.             }
  316.            
  317.             if ($scheme = https) {
  318.                 more_set_headers                        "Strict-Transport-Security: max-age=15724800; includeSubDomains";
  319.             }
  320.            
  321.             access_log off;
  322.            
  323.             port_in_redirect off;
  324.            
  325.             set $balancer_ewma_score -1;
  326.             set $proxy_upstream_name    "upstream-default-backend";
  327.             set $proxy_host             $proxy_upstream_name;
  328.            
  329.             set $proxy_alternative_upstream_name "";
  330.            
  331.             client_max_body_size                    1m;
  332.            
  333.             proxy_set_header Host                   $best_http_host;
  334.            
  335.             # Pass the extracted client certificate to the backend
  336.            
  337.             # Allow websocket connections
  338.             proxy_set_header                        Upgrade           $http_upgrade;
  339.            
  340.             proxy_set_header                        Connection        $connection_upgrade;
  341.            
  342.             proxy_set_header X-Request-ID           $req_id;
  343.             proxy_set_header X-Real-IP              $the_real_ip;
  344.            
  345.             proxy_set_header X-Forwarded-For        $the_real_ip;
  346.            
  347.             proxy_set_header X-Forwarded-Host       $best_http_host;
  348.             proxy_set_header X-Forwarded-Port       $pass_port;
  349.             proxy_set_header X-Forwarded-Proto      $pass_access_scheme;
  350.            
  351.             proxy_set_header X-Original-URI         $request_uri;
  352.            
  353.             proxy_set_header X-Scheme               $pass_access_scheme;
  354.            
  355.             # Pass the original X-Forwarded-For
  356.             proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;
  357.            
  358.             # mitigate HTTPoxy Vulnerability
  359.             # https://www.nginx.com/blog/mitigating-the-httpoxy-vulnerability-with-nginx/
  360.             proxy_set_header Proxy                  "";
  361.            
  362.             # Custom headers to proxied server
  363.            
  364.             proxy_connect_timeout                   5s;
  365.             proxy_send_timeout                      60s;
  366.             proxy_read_timeout                      60s;
  367.            
  368.             proxy_buffering                         off;
  369.             proxy_buffer_size                       4k;
  370.             proxy_buffers                           4 4k;
  371.             proxy_request_buffering                 on;
  372.            
  373.             proxy_http_version                      1.1;
  374.            
  375.             proxy_cookie_domain                     off;
  376.             proxy_cookie_path                       off;
  377.            
  378.             # In case of errors try the next upstream server before returning an error
  379.             proxy_next_upstream                     error timeout;
  380.             proxy_next_upstream_timeout             0;
  381.             proxy_next_upstream_tries               3;
  382.            
  383.             proxy_pass http://upstream_balancer;
  384.            
  385.             proxy_redirect                          off;
  386.            
  387.         }
  388.        
  389.         # health checks in cloud providers require the use of port 80
  390.         location /healthz {
  391.            
  392.             access_log off;
  393.             return 200;
  394.         }
  395.        
  396.         # this is required to avoid error if nginx is being monitored
  397.         # with an external software (like sysdig)
  398.         location /nginx_status {
  399.            
  400.             allow 127.0.0.1;
  401.            
  402.             allow ::1;
  403.            
  404.             deny all;
  405.            
  406.             access_log off;
  407.             stub_status on;
  408.         }
  409.        
  410.     }
  411.     ## end server _
  412.    
  413.     ## start server fs.example.com
  414.     server {
  415.         server_name fs.example.com ;
  416.        
  417.         listen 80;
  418.        
  419.         listen [::]:80;
  420.        
  421.         set $proxy_upstream_name "-";
  422.         set $pass_access_scheme $scheme;
  423.         set $pass_server_port $server_port;
  424.         set $best_http_host $http_host;
  425.         set $pass_port $pass_server_port;
  426.        
  427.         location / {
  428.            
  429.             set $namespace      "yunwei";
  430.             set $ingress_name   "fs1";
  431.             set $service_name   "fs";
  432.             set $service_port   "web";
  433.             set $location_path  "/";
  434.            
  435.             rewrite_by_lua_block {
  436.                 lua_ingress.rewrite({
  437.                     force_ssl_redirect = false,
  438.                     use_port_in_redirects = false,
  439.                 })
  440.                 balancer.rewrite()
  441.                 plugins.run()
  442.             }
  443.            
  444.             header_filter_by_lua_block {
  445.                
  446.                 plugins.run()
  447.             }
  448.             body_filter_by_lua_block {
  449.                
  450.             }
  451.            
  452.             log_by_lua_block {
  453.                
  454.                 balancer.log()
  455.                
  456.                 monitor.call()
  457.                
  458.                 plugins.run()
  459.             }
  460.            
  461.             port_in_redirect off;
  462.            
  463.             set $balancer_ewma_score -1;
  464.             set $proxy_upstream_name    "yunwei-fs-web";
  465.             set $proxy_host             $proxy_upstream_name;
  466.            
  467.             set $proxy_alternative_upstream_name "";
  468.            
  469.             client_max_body_size                    1m;
  470.            
  471.             proxy_set_header Host                   $best_http_host;
  472.            
  473.             # Pass the extracted client certificate to the backend
  474.            
  475.             # Allow websocket connections
  476.             proxy_set_header                        Upgrade           $http_upgrade;
  477.            
  478.             proxy_set_header                        Connection        $connection_upgrade;
  479.            
  480.             proxy_set_header X-Request-ID           $req_id;
  481.             proxy_set_header X-Real-IP              $the_real_ip;
  482.            
  483.             proxy_set_header X-Forwarded-For        $the_real_ip;
  484.            
  485.             proxy_set_header X-Forwarded-Host       $best_http_host;
  486.             proxy_set_header X-Forwarded-Port       $pass_port;
  487.             proxy_set_header X-Forwarded-Proto      $pass_access_scheme;
  488.            
  489.             proxy_set_header X-Original-URI         $request_uri;
  490.            
  491.             proxy_set_header X-Scheme               $pass_access_scheme;
  492.            
  493.             # Pass the original X-Forwarded-For
  494.             proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;
  495.            
  496.             # mitigate HTTPoxy Vulnerability
  497.             # https://www.nginx.com/blog/mitigating-the-httpoxy-vulnerability-with-nginx/
  498.             proxy_set_header Proxy                  "";
  499.            
  500.             # Custom headers to proxied server
  501.            
  502.             proxy_connect_timeout                   5s;
  503.             proxy_send_timeout                      60s;
  504.             proxy_read_timeout                      60s;
  505.            
  506.             proxy_buffering                         off;
  507.             proxy_buffer_size                       4k;
  508.             proxy_buffers                           4 4k;
  509.             proxy_request_buffering                 on;
  510.            
  511.             proxy_http_version                      1.1;
  512.            
  513.             proxy_cookie_domain                     off;
  514.             proxy_cookie_path                       off;
  515.            
  516.             # In case of errors try the next upstream server before returning an error
  517.             proxy_next_upstream                     error timeout;
  518.             proxy_next_upstream_timeout             0;
  519.             proxy_next_upstream_tries               3;
  520.            
  521.             proxy_pass http://upstream_balancer;
  522.            
  523.             proxy_redirect                          off;
  524.            
  525.         }
  526.        
  527.     }
  528.     ## end server fs.example.com
  529.    
  530.     # backend for when default-backend-service is not configured or it does not have endpoints
  531.     server {
  532.         listen 8181 default_server reuseport backlog=32768;
  533.         listen [::]:8181 default_server reuseport backlog=32768;
  534.         set $proxy_upstream_name "internal";
  535.        
  536.         access_log off;
  537.        
  538.         location / {
  539.             return 404;
  540.         }
  541.     }
  542.    
  543.     # default server, used for NGINX healthcheck and access to nginx stats
  544.     server {
  545.         listen unix:/tmp/nginx-status-server.sock;
  546.         set $proxy_upstream_name "internal";
  547.        
  548.         keepalive_timeout 0;
  549.         gzip off;
  550.        
  551.         access_log off;
  552.        
  553.         location /healthz {
  554.             return 200;
  555.         }
  556.        
  557.         location /is-dynamic-lb-initialized {
  558.             content_by_lua_block {
  559.                 local configuration = require("configuration")
  560.                 local backend_data = configuration.get_backends_data()
  561.                 if not backend_data then
  562.                 ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
  563.                 return
  564.                 end
  565.                
  566.                 ngx.say("OK")
  567.                 ngx.exit(ngx.HTTP_OK)
  568.             }
  569.         }
  570.        
  571.         location /nginx_status {
  572.             stub_status on;
  573.         }
  574.        
  575.         location /configuration {
  576.             # this should be equals to configuration_data dict
  577.             client_max_body_size                    10m;
  578.             client_body_buffer_size                 10m;
  579.             proxy_buffering                         off;
  580.            
  581.             content_by_lua_block {
  582.                 configuration.call()
  583.             }
  584.         }
  585.        
  586.         location / {
  587.             content_by_lua_block {
  588.                 ngx.exit(ngx.HTTP_NOT_FOUND)
  589.             }
  590.         }
  591.     }
  592. }
  593.  
  594. stream {
  595.     lua_package_cpath "/usr/local/lib/lua/?.so;/usr/lib/lua-platform-path/lua/5.1/?.so;;";
  596.     lua_package_path "/etc/nginx/lua/?.lua;/etc/nginx/lua/vendor/?.lua;/usr/local/lib/lua/?.lua;;";
  597.    
  598.     lua_shared_dict tcp_udp_configuration_data 5M;
  599.    
  600.     init_by_lua_block {
  601.         collectgarbage("collect")
  602.        
  603.         -- init modules
  604.         local ok, res
  605.        
  606.         ok, res = pcall(require, "configuration")
  607.         if not ok then
  608.         error("require failed: " .. tostring(res))
  609.         else
  610.         configuration = res
  611.     configuration.nameservers = { "180.76.76.76", "172.31.90.17" }
  612.         end
  613.        
  614.         ok, res = pcall(require, "tcp_udp_configuration")
  615.         if not ok then
  616.         error("require failed: " .. tostring(res))
  617.         else
  618.         tcp_udp_configuration = res
  619.         end
  620.        
  621.         ok, res = pcall(require, "tcp_udp_balancer")
  622.         if not ok then
  623.         error("require failed: " .. tostring(res))
  624.         else
  625.         tcp_udp_balancer = res
  626.         end
  627.     }
  628.    
  629.     init_worker_by_lua_block {
  630.         tcp_udp_balancer.init_worker()
  631.     }
  632.    
  633.     lua_add_variable $proxy_upstream_name;
  634.    
  635.     log_format log_stream [$time_local] $protocol $status $bytes_sent $bytes_received $session_time;
  636.    
  637.     access_log /var/log/nginx/access.log log_stream ;
  638.    
  639.     error_log  /var/log/nginx/error.log;
  640.    
  641.     upstream upstream_balancer {
  642.         server 0.0.0.1:1234; # placeholder
  643.        
  644.         balancer_by_lua_block {
  645.             tcp_udp_balancer.balance()
  646.         }
  647.     }
  648.    
  649.     server {
  650.         listen unix:/tmp/ingress-stream.sock;
  651.        
  652.         content_by_lua_block {
  653.             tcp_udp_configuration.call()
  654.         }
  655.     }
  656.    
  657.     # TCP services
  658.    
  659.     # UDP services
  660.    
  661. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement