[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Doing-code/guide/main/JavaGuide/Nginx.md [Back]  [Original]

# Nginx ## ### Nginx #### Nginx 1. Nginxmasterworker workermasterworker 2. Nginx masterworkerworkermasterworker 3. PID `ps -ef | grep nginx` 4. | | | | -------- | ----------------------------------- | | TERM/INT | Nginx | | QUIT | Nginx | | HUP | | | USR1 | | | USR2 | Nginx | | WINCH | workerQUIT | 5. Nginx `kill -signal PID`signalTERMQUITWINCHPIDmasterID ```shell kill -TERM PID kill -QUIT PID ``` #### Nginx ```shell ./nginx ./nginx -s stop ./nginx -s reload ./nginx -t ``` ### Nginx > https://www.bilibili.com/video/BV1ov41187bq?p=20 nginxnginxnginx #### Nginx - eventshttpNginx - eventsnginx - http... ```conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } ``` `https://www.bilibili.com/video/BV1ov41187bq?p=22` ## server_name 1. server_name 2. server_name 3. server_name 4. server_name 5. default_serverdefault_serverserver ### #### sendfile - sendfile on | off - sendfile off - httpserverlocation http #### tcp_nopush - tcp_nopush on | off - tcp_nopush off - httpserverlocation sendfile #### tcp_nodeplay - tcp_nodelay on | off - tcp_nodelay on - httpserverlocation keep-alive ### > https://www.bilibili.com/video/BV1ov41187bq?p=51 gzip on | off gzip`gzip_types application/javascript text/html` `gzip_comp_level 6;`6 Gzip ```conf gzip on; # Gzip gzip_types *; # , gzip_comp_level 6; # Gzip gzip_min_length 1k; # content-length gzip_buffers 4 16K; # gzip_http_version 1.1; # HTTP gzip_vary on; # gzip_disable "MSIE [1-6]\."; # IE6 gzip_proxied off; # Nginx ``` Gzipsendfile sendfileGzip gzip gzip_static on | off | alwaysoff gzip_static ngx_http_gzip_static_module ### Expires ```conf location ~ .*\.(html|js|css|png|jpg|jpeg|gif)$ { # ... expires max # ... } #expires 30s; # 30 #expires 30m; # 30 #expires 10h; # 10 #expires 1d; # 1 ``` Cache-Control ```conf add_header Cache-control must-revalidate; # add_header Cache-control no-cache; # max-age add_header Cache-control no-store; # () add_header Cache-control no-transform; # add_header Cache-control public; # : add_header Cache-control private; # max-age add_header Cache-control proxy-revalidate; # add_header Cache-Control max-age=; # , add_header Cache-control s-maxage=; # , Age ``` ### Nginx ```conf location /getUser { # Access-Control-Allow-Origin() * add_header Access-Control-Allow-Origin *; # Access-Control-Allow-Methods GETPOSTPUTDELETE ...... add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE; default_type application/json; # return json return 200 '{"id":1,"name":"TOM","age":18}'; } ``` ### Nginx Web Referer Referer 403 ```conf location ~ *\.(png|jpg|gif){ valid_referers none blocked www.baidu.com 192.168.91.200; # valid_referers none blocked *.example.com example.* www.example.org ~\.google\.; if ($invalid_referer){ return 403; } root /usr/local/nginx/html; } # location /images { valid_referers none blocked www.baidu.com 192.168.199.27; # valid_referers none blocked *.example.com example.* www.example.org ~\.google\.; if ($invalid_referer){ return 403; } root /usr/local/nginx/html; } ``` www.baidu.com 192.168.91.200 $invalid_referer 1true 403 Referer Referer ### Rewrite > https://www.bilibili.com/video/BV1ov41187bq?p=70 Rewrite - set - `set ;` ```conf server { listen 8081; server_name localhost; location /server { set $name TOM; set $age 18; default_type text/plain; return 200 $name=$age; } } ``` - if: Nginx ```conf if ($request_method = POST){ return 405; } ``` - break Nginx URIlocation - return return Nginx - ```conf return [text]; return ; return ; code HTTP 0 ~ 999 HTTP text JSON URL URL ``` - rewrite URI URL - `rewrite regex replacement [flag];` - regex URI - replacement URI http:// https:// URI URI - flag Rewrite URI ```conf # ...... listen 8081; location /rewrite { rewrite ^/rewrite/url\w*$ https://www.baidu.com; # $1 ^ rewrite ^/rewrite/(test)\w*$ /$1; # /rewrite/testxxx url test rewrite ^/rewrite/(demo)\w*$ /$1; # /rewrite/demoxxx url demo } location /test { # url test location default_type text/plain; return 200 test_sucess; } location /demo { # url demo location default_type text/plain; return 200 demo_sucess; } ``` - rewrite_log URL URL notice error_log ```conf location /rewrite_log { rewrite_log on; # error_log logs /error.log notice; # notice return 200 ''; } ``` Rewrite - - - - `/` ```conf server { listen 80; server_name localhost; server_name_in_redirect on; location /frx { if (-d $request_filename){ # rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; # $2 / } } } ``` - - ```conf # server{ listen 80; server_name www.web.com; locatin ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked server_names *.web.com; # server_names IP if ($invalid_referer){ rewrite ^/ http://www.web.com/images/forbidden.png; # } } } # server{ listen 80; server_name www.web.com; location /file { root /server/file; # server file valid_referers none blocked server_names *.web.com; # server_names IP if ($invalid_referer){ rewrite ^/ http://www.web.com/images/forbidden.png; # file } } } ``` - : - - - - request - #### Rewrite | | | | ------------------ | -------------------------------------------------------------------------------------------------------------------------------- | | $args | URL `http://192.168.200.133:8080?arg1=value1&args2=value2` arg1=value1&arg2=value2 $query_string | | $http_user_agent | | | $host | server_name | | $document_uri | URI `http://192.168.200.133/server?id=10&name=zhangsan` /server $uri | | $document_root | location root Nginx html | | $content_length | Content-Length | | $content_type | Content-Type | | $http_cookie | cookie add_header Set-Cookie 'cookieName=cookieValue' cookie | | $limit_rate | Nginx Nginx limit_rate 0 | | $remote_addr | IP | | $remote_port | | | $remote_user | | | $scheme | | | $server_addr | | | $server_name | | | $server_port | | | $server_protocol | HTTP/1.1 | | $request_body_file | | | $request_method | GET , POST | | $request_filename | | | $request_uri | URI `http://192.168.200.133/server?id=10&name=zhangsan` /server?id=10&name=zhangsan | ### ```conf # DOS limit_conn_zone $binary_remote_addr zone=perip:10m; limit_conn_zone $server_name zone=perserver:10m; # ip limit_conn perip 2; # server limit_conn perserver 20; # limit_rate 300k; ``` ### ```conf # server_names_hash_bucket_size 128; server_names_hash_max_size 512; # keepalive_timeout 65; client_header_timeout 15s; client_body_timeout 15s; send_timeout 60s; # # 75 proxy_connect_timeout 30s; proxy_send_timeout 120s; # proxy_read_timeout 120s; ``` ### - - - proxy_pass - `proxy_pass ;` ```conf # location /server { # proxy_pass http://192.168.200.146; # http://192.168.200.146/server/index.html # proxy_pass http://192.168.200.146/; # http://192.168.200.146/index.html } ``` - proxy_set_header ```conf # server { listen 8080; server_name localhost; default_type text/plain; return 200 $http_username; # http username } # server { listen 8080; server_name localhost; location /server { # /server proxy_pass http://192.168.200.146:8080/; # B proxy_set_header username TOM; # key usernamevalue TOM B } } ``` - proxy_redirect - ```conf # redirect Location # replacement Location proxy_redirect redirect replacement; proxy_redirect default; proxy_redirect off; ``` - `proxy_redirect` - Location Refresh ```conf # server { listen 8081; server_name localhost; if (!-f $request_filename){ return 302 http://192.168.200.146; # 2. B } } # server { listen 8081; server_name localhost; location / { proxy_pass http://192.168.200.146:8081/; # 1. B proxy_redirect http://192.168.200.146 http://192.168.200.133; # 3. B } } # server B server { listen 80; server_name 192.168.200.133; location / { proxy_pass http://192.168.200.146; # 4. B } } ``` proxy_pass `/` ```conf server { listen 80; server_name localhost; location / { # location / proxy_pass http://192.168.200.146; proxy_pass http://192.168.200.146/; } } server{ listen 80; server_name localhost; location /server { # location /server / #proxy_pass http://192.168.200.146; proxy_pass http://192.168.200.146/; } } # location /server http://localhost/server/index.html # proxy_pass http://localhost/server/index.html # proxy_pass http://localhost/index.html ``` / location /server proxy_pass / /server / /server /server / /server / / > https://frxcat.fun/middleware/Nginx/Nginx_Reverse_proxy/#%E6%96%9C%E6%9D%A0%E6%80%BB%E7%BB%93 #### NginxSSL SSL`--with-http_ssl_module`sslHTTPS`listen 443 ssl; ssl on | off;` HTTPS HTTP SSL/TLS ##### SSL - `ssl_certificate` PEM - `ssl_certificate ;` - `ssl_certificate_key` PEM secret key - `ssl_ceritificate_key ;` - `ssl_session_cache` SSL - `ssl_sesion_cache ` - offNginx - noneNginx - builtin OpenSSL 20480 - shared name size name name size ```conf ssl_session_cache builtin:1000 shared:SSL:10m; ``` - `ssl_session_timeout` SSL 5 - `ssl_session_timeout ;` - `ssl_ciphers` OpenSSL - `ssl_ciphers ;` `openssl ciphers`OpenSSl - `ssl_prefer_server_ciphers` - `ssl_perfer_server_ciphers ;` ##### SSL ```conf server { listen 80; # ...... } server { listen 443 ssl; # SSL server_name localhost; # ssl_certificate /root/cert/server.cert; # cert pem ssl_certificate_key /root/cert/server.key; # key ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; # ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; # TLS ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } } ``` `http://` ```conf server { listen 443 ssl; server_name www.test.com; # www.test.com location / { # ...... rewrite ^(.*)$ https://www.test.com$1; # https # ...... } # ...... } ``` #### - `proxy_buffering` - `proxy_buffering ;` - `proxy_buffers` - `proxy_buffers ;` - numbersize number * size - `proxy_buffers 8 4k | 8k` - `proxy_buffer_size` proxy_buffers size - `proxy_buffer_size ;``proxy_buffers 8 4k | 8k` - `proxy_busy_buffers_size` BUSY - `proxy_busy_buffers_size ;``proxy_busy_buffers_size 8k | 16K;` - `proxy_temp_path` Nginx - `proxy_temp_path ;``proxy_temp_path proxy_temp;`path - `proxy_temp_file_write_size` - `proxy_temp_file_write_size ;``proxy_temp_file_write_size 8K | 16K;` ```conf proxy_buffering on; proxy_buffers 4 64k; proxy_buffer_size 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; ``` #### ```conf server { listen 80; server_name localhost; location ~ .* { # proxy_pass http://127.0.0.1:8081; # # proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` ### - OSIip+port - URLip - - - (LVS) + (Nginx) #### Nginx proxy_pass Nginx Nginx Nginx upstream - upstream 1 TCP Unix socket - `upstream {ip+port;ip+port ...}` - serverIP Unix socket - `server [paramerters]` - server name upstream name - ```conf # 1 server { listen 9001; server_name localhost; default_type text/html; location /{ return 200 '

192.168.200.146:9001

'; } } # 2 server { listen 9002; server_name localhost; default_type text/html; location /{ return 200 '

192.168.200.146:9002

'; } } # 3 server { listen 9003; server_name localhost; default_type text/html; location / { return 200 '

192.168.200.146:9003

'; } } # upstream backend{ server 192.168.200.146:9091; server 192.168.200.146:9092; server 192.168.200.146:9093; } server { listen 8083; server_name localhost; location / { proxy_pass http://backend; # backend upstream } } ``` `http://192.168.200.133:8083`proxy_passbackendupstreambackend ```text proxy_pass http://192.168.200.146:9091 proxy_pass http://192.168.200.146:9092 proxy_pass http://192.168.200.146:9093 ``` #### - down server - backup - down down backup - max_fails 1 - fail_timeout max_fails 10 - max_cons upstream 0 ```conf upstream backend{ # server 192.168.200.146:9001 down; # server 192.168.200.146:9002 backup; # 2 server 192.168.200.146:9003 max_conns=2; # 15 server 192.168.200.146:9003 max_fails=3 fail_timeout=15; } server { listen 8083; server_name localhost; location / { proxy_pass http://backend; } } ``` #### - - weight 1 - ip_hashIP ip_hash - least_conn - url_hashURL - fairfair`nginx-upstream-fair` ```conf upstream backend{ # server 192.168.200.146:9001 weight=10; # ip_hash; # least_conn; # hash &request_uri; # fair; server 192.168.200.146:9002; server 192.168.200.146:9003; } server { listen 8083; server_name localhost; location /{ proxy_pass http://backend; } } ``` #### Nginx1.9streamhttp LVSHAProxyF5Nginx Nginxstream`--with-stream` httpstreamstream http upstream ```conf http { server { listen 80; # ...... } } stream { upstream backend{ server 192.168.200.146:6379; server 192.168.200.146:6378; } server { listen 81; proxy_pass backend; } } ``` ### Nginx Nginx Web `ngx_http_proxy_module` #### - `proxy_cache_path` - `proxy_cache_path [levels=number] [inactive=time][max_size=size];` - levels path path 3 1 2 MD5 - keys_zone - inactiveinactive 10 - max_size ```conf # proxy_cache_key kele MD5 27ce47ea65c1381dbe5175f7c77d8a3a # levels=1:2 # /usr/local/proxy_cache/a/a3 1:2 # levels=2:1:2 # /usr/local/proxy_cache/3a/a/d8 2:1:2 # levels=2:2:2 # /usr/local/proxy_cache/3a/8a/7d 2:2:2 # keys_zone=kele:200m # kele 200M1M 8000 keys # inactive=1d # 1 # max_size=20g # 20G http{ proxy_cache_path /usr/local/proxy_cache keys_zone=kele:200m levels=1:2:1 inactive=1d max_size=20g; } ``` - `proxy_cache` - `proxy_cache ;` - `zone_name` proxy_cache_path keys_zone - `proxy_cache_key` Web key Nginx key MD5 - `proxy_cache_key ;` - `proxy_cache_valid` URL - `proxy_cache_valid [code ...... ] ;` ```conf proxy_cache_valid 200 302 10m; # 200 302 URL 10 proxy_cache_valid 404 1m; # 404 URL 1 proxy_cache_valid any 1m; # URL 1 ``` - `proxy_cache_min_uses` 1 - `proxy_cache_min_uses ;` - `proxy_cache_methods` HTTP HTTP GET HEAD POST - `proxy_cache_methods ;` #### ```conf # http{ proxy_cache_path /usr/local/proxy_cache levels=2:1 keys_zone=bing:200m inactive=1d max_size=20g; upstream backend{ server 192.168.200.146:8080; # A } server { listen 8080; # 8080 server_name localhost; # localhost IP location / { # / proxy_cache bing; # bing 2 keys_zone proxy_cache_key kele; # key MD5 proxy_cache_min_uses 5; # 5 proxy_cache_valid 200 5d; # 200 URL 5 proxy_cache_valid 404 30s; # 404 URL 30 proxy_cache_valid any 1m; # URL 1 add_header nginx-cache "$upstream_cache_status"; # proxy_pass http://backend/js/; # backend /js/ backend } } } ``` #### - ```shell # /usr/local/proxy_cache/ rm -rf /usr/local/proxy_cache/ ``` - `ngx_cache_purge` `proxy_cache_purge `cache proxy_cachekey proxy_cache_key ```conf server{ location ~/purge(/.*) { proxy_cache_purge bing kele; } } ``` #### - `proxy_no_cache` - `proxy_no_cache ...... ;` string - `proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;` - `proxy_cache_bypass` - `proxy_cache_bypass ...... ;` - `proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;` 0 ```conf server { listen 8080; server_name localhost; location / { if ($request_uri ~ /.*\.js$){ set $nocache 1; } proxy_no_cache $nocache $cookie_nocache $arg_nocache $arg_comment; proxy_cache_bypass $nocache $cookie_nocache $arg_nocache $arg_comment; } } ``` `$nocache $cookie_nocache $arg_nocache $arg_comment` 0 ### Nginx NginxNginxNginx Nginx Nginx IP Keepalived KeepalivedVRRP KeepalivedipipipmasterNginxmasterNginxBackupNginx > https://www.bilibili.com/video/BV1ov41187bq?p=128 ### Nginx& ```conf location /download { root /opt; # location /download /opt # txt if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|conf)$){ add_header Content-Disposition 'attachment;'; } autoindex on; # autoindex_exact_size on; # autoindex_format html; # html autoindex_localtime on; # } ``` root /opt/download /opt location /download > https://www.bilibili.com/video/BV1ov41187bq?p=140 ### lua > luahttp://www.lua.org/manual/5.4/ ## ### nginxrootalias rootalias - rootroot + location - aliaslocationnginxaliaslocation ### nginx
Web Proxy Viewer  |  New URL  |  Original Page