标签:nginx
nginx
参考文档
官方文档:http://nginx.org/en/docs/
编译安装nginx
#安装依赖环境
yum install -y gcc-c++ gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-devel perl-ExtUtils-Embed
#下载并解压包
wget http://nginx.org/download/nginx-1.13.9.tar.gz
tar axf nginx-1.13.9.tar.gz
编译安装
./configure --prefix=/usr/local/nginx \ --with-openssl=/usr/include \ --with-pcre \ --with-http_stub_status_module make &&make install && echo $?  | 
nginx静态页面
cd /usr/local/nginx/conf
cp nginx.conf{,.bak}
vim /usr/local/nginx/conf/nginx.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 nginx1; #"jpg文件访问目录,绝对路径为/usr/local/nginx/html/jpg" #"autoindex on 显示目录列表" location /jpg { root html; autoindex on; } #"txt文件存放目录,绝对路径为/data/txt" location /txt { root /data/; autoindex on; } #"md文件存放目录,绝对路径为/data2" location /md { alias /data2/; autoindex on; } #"jsp文件存放目录,绝对路径为/jsp" location /jsp { alias /jsp/; autoindex on; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }  | 
启动nginx
cd /usr/local/nginx/sbin ./nginx  | 
nginx负载均衡
再安装两个nginx,一个用于静态页面访问,一个用于负载均衡
官方文档:http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html
安装
静态网站同上,注意修改监听端口
负载均衡
cd nginx-1.13.9
./configure --prefix=/usr/local/nginx_proxy \ --with-http_ssl_module \ --with-http_perl_module \ --with-pcre \ --with-http_stub_status_module \ && echo $?  | 
| make &&make install && echo $? | 
#"修改配置文件"
cd /usr/local/nginx_proxy/conf
vim nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; client_max_body_size 50m; upstream test_server { server 192.168.3.58:8001 weight=5; server 192.168.3.58:80 server 192.168.3.58:8080 backup; # keepalive 8; } server { listen 8000; server_name nginx_proxy; root /usr/local/nginx1/html; #"proxy_set_header重新定义或添加字段传递给代理服务器的请求头。" #"通过代理服务器传递服务器名称和端口" proxy_set_header Host $host:$server_port; #"获取客户端IP" proxy_set_header X-Real-IP $remote_addr; #"获取客户端访问端口" proxy_set_header X-Real-PORT $remote_port; #"获取代理服务器ip" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://test_server; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }  | 
启动nginx
cd /usr/local/nginx_proxy/sbin ./nginx  | 
nginx配置选项
文档:http://nginx.org/en/docs/http/ngx_http_upstream_module.html
指令  | 用法 | 可选项  | 
upstream  | Syntax:	upstream name { ... } Default: — Context: http  | 
 
 
 
 
 
 
 
 
 
  | 
| zone | Syntax: zone name [size]; Default: — Context: upstream This directive appeared in version 1.9.0.  | |
| state | Syntax: state file; Default: — Context: upstream This directive appeared in version 1.9.7.  | |
| hash | Syntax: hash key [consistent]; Default: — Context: upstream This directive appeared in version 1.7.2.  | |
| ip_hash | Syntax:	ip_hash; Default: — Context: upstream  | |
| keepalive | Syntax: keepalive connections; Default: — Context: upstream This directive appeared in version 1.1.4.  | |
| ntlm | Syntax: ntlm; Default: — Context: upstream This directive appeared in version 1.9.2.  | |
| least_conn | Syntax: least_conn; Default: — Context: upstream This directive appeared in versions 1.3.1 and 1.2.2.  | |
| least_time | Syntax: least_time header | last_byte [inflight]; Default: — Context: upstream This directive appeared in version 1.7.10.  | |
| queue | Syntax: queue number [timeout=time]; Default: — Context: upstream This directive appeared in version 1.5.12.  | |
| sticky | Syntax: sticky cookie name [expires=time] [domain=domain] [httponly] [secure] [path=path]; sticky route $variable ...; sticky learn create=$variable lookup=$variable zone=name:size [timeout=time] [header]; Default: — Context: upstream This directive appeared in version 1.5.7.  | cookie route learn 嵌入变量 $upstream_addr $upstream_bytes_received $upstream_cache_status $upstream_connect_time 
 
 
 
 
 
  | 
rewrite_module官方文档:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
| rewrite | Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if  | |
| break | Syntax: break; Default: — Context: server, location, if  | |
| if | Syntax: if (condition) { ... } Default: — Context: server, location  | |
| return | Syntax: return code [text]; return code URL; return URL; Default: — Context: server, location, if  | |
| rewrite_log | Syntax: rewrite_log on | off; Default: rewrite_log off; Context: http, server, location, if  | |
| set | Syntax: set $variable value; Default: — Context: server, location, if  | |
| uninitialized_variable_warn | Syntax: uninitialized_variable_warn on | off; Default: uninitialized_variable_warn on; Context: http, server, location, if  | 
proxy_module官方文档:http://nginx.org/en/docs/http/ngx_http_proxy_module.html
| proxy_pass | Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except  | |
| proxy_bind | Syntax: proxy_bind address [transparent] | off; Default: — Context: http, server, location This directive appeared in version 0.8.22.  | |
| proxy_buffer_size | Syntax: proxy_buffer_size size; Default: proxy_buffer_size 4k|8k; Context: http, server, location  | |
| proxy_buffering | Syntax: proxy_buffering on | off; Default: proxy_buffering on; Context: http, server, location  | |
| proxy_buffers | Syntax: proxy_buffers number size; Default: proxy_buffers 8 4k|8k; Context: http, server, location  | |
| proxy_busy_buffers_size | Syntax: proxy_busy_buffers_size size; Default: proxy_busy_buffers_size 8k|16k; Context: http, server, location  | |
| proxy_cache | Syntax: proxy_cache zone | off; Default: proxy_cache off; Context: http, server, location  | |
| proxy_cache_background_update | Syntax: proxy_cache_background_update on | off; Default: proxy_cache_background_update off; Context: http, server, location This directive appeared in version 1.11.10.  | |
| proxy_cache_bypass | Syntax: proxy_cache_bypass string ...; Default: — Context: http, server, location  | |
| proxy_cache_convert_head | Syntax: proxy_cache_convert_head on | off; Default: proxy_cache_convert_head on; Context: http, server, location This directive appeared in version 1.9.7.  | |
| proxy_cache_key | Syntax: proxy_cache_key string; Default: proxy_cache_key $scheme$proxy_host$request_uri; Context: http, server, location  | |
| proxy_cache_lock | Syntax: proxy_cache_lock on | off; Default: proxy_cache_lock off; Context: http, server, location This directive appeared in version 1.1.12.  | |
| proxy_cache_lock_age | Syntax: proxy_cache_lock_age time; Default: proxy_cache_lock_age 5s; Context: http, server, location This directive appeared in version 1.7.8.  | |
| proxy_cache_lock_timeout | Syntax: proxy_cache_lock_timeout time; Default: proxy_cache_lock_timeout 5s; Context: http, server, location This directive appeared in version 1.1.12.  | |
| proxy_cache_max_range_offset | Syntax: proxy_cache_max_range_offset number; Default: — Context: http, server, location This directive appeared in version 1.11.6.  | |
| proxy_cache_methods | Syntax: proxy_cache_methods GET | HEAD | POST ...; Default: proxy_cache_methods GET HEAD; Context: http, server, location This directive appeared in version 0.7.59.  | |
| proxy_cache_min_uses | Syntax: proxy_cache_min_uses number; Default: proxy_cache_min_uses 1; Context: http, server, location  | |
| proxy_cache_path | Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; Default: — Context: http  | 
 
 
 
  | 
| proxy_cache_purge | Syntax: proxy_cache_purge string ...; Default: — Context: http, server, location This directive appeared in version 1.5.7.  | |
| proxy_cache_revalidate | Syntax: proxy_cache_revalidate on | off; Default: proxy_cache_revalidate off; Context: http, server, location This directive appeared in version 1.5.7.  | |
| proxy_cache_use_stale | Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...; Default: proxy_cache_use_stale off; Context: http, server, location  | |
| proxy_cache_valid | Syntax: proxy_cache_valid [code ...] time; Default: — Context: http, server, location  | |
| proxy_connect_timeout | Syntax: proxy_connect_timeout time; Default: proxy_connect_timeout 60s; Context: http, server, location  | |
| proxy_cookie_domain | Syntax: proxy_cookie_domain off; proxy_cookie_domain domain replacement; Default: proxy_cookie_domain off; Context: http, server, location This directive appeared in version 1.1.15.  | |
| proxy_cookie_path | Syntax: proxy_cookie_path off; proxy_cookie_path path replacement; Default: proxy_cookie_path off; Context: http, server, location This directive appeared in version 1.1.15.  | |
| proxy_force_ranges | Syntax: proxy_force_ranges on | off; Default: proxy_force_ranges off; Context: http, server, location This directive appeared in version 1.7.7.  | |
| proxy_headers_hash_bucket_size | Syntax: proxy_headers_hash_bucket_size size; Default: proxy_headers_hash_bucket_size 64; Context: http, server, location  | |
| proxy_headers_hash_max_size | Syntax: proxy_headers_hash_max_size size; Default: proxy_headers_hash_max_size 512; Context: http, server, location  | |
| proxy_hide_header | Syntax: proxy_hide_header field; Default: — Context: http, server, location  | |
| proxy_http_version | Syntax: proxy_http_version 1.0 | 1.1; Default: proxy_http_version 1.0; Context: http, server, location This directive appeared in version 1.1.4.  | |
| proxy_ignore_client_abort | Syntax: proxy_ignore_client_abort on | off; Default: proxy_ignore_client_abort off; Context: http, server, location  | |
| proxy_ignore_headers | Syntax: proxy_ignore_headers field ...; Default: — Context: http, server, location  | |
| proxy_intercept_errors | Syntax: proxy_intercept_errors on | off; Default: proxy_intercept_errors off; Context: http, server, location  | |
| proxy_limit_rate | Syntax: proxy_limit_rate rate; Default: proxy_limit_rate 0; Context: http, server, location This directive appeared in version 1.7.7.  | |
| proxy_max_temp_file_size | Syntax: proxy_max_temp_file_size size; Default: proxy_max_temp_file_size 1024m; Context: http, server, location  | |
| proxy_method | Syntax: proxy_method method; Default: — Context: http, server, location  | |
| proxy_next_upstream | Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...; Default: proxy_next_upstream error timeout; Context: http, server, location  | error timeout invalid_header http_500 http_502 http_404 non_idempotent off  | 
| proxy_next_upstream_timeout | Syntax: proxy_next_upstream_timeout time; Default: proxy_next_upstream_timeout 0; Context: http, server, location This directive appeared in version 1.7.5.  | |
| proxy_next_upstream_tries | Syntax: proxy_next_upstream_tries number; Default: proxy_next_upstream_tries 0; Context: http, server, location This directive appeared in version 1.7.5.  | |
| proxy_no_cache | Syntax: proxy_no_cache string ...; Default: — Context: http, server, location  | |
| proxy_pass | Syntax: proxy_pass URL; Default: — Context: location, if in location, limit_except  | |
| proxy_pass_header | Syntax: proxy_pass_header field; Default: — Context: http, server, location  | |
| proxy_pass_request_body | Syntax: proxy_pass_request_body on | off; Default: proxy_pass_request_body on; Context: http, server, location  | |
| proxy_pass_request_headers | Syntax: proxy_pass_request_headers on | off; Default: proxy_pass_request_headers on; Context: http, server, location  | |
| proxy_read_timeout | Syntax: proxy_read_timeout time; Default: proxy_read_timeout 60s; Context: http, server, location  | |
| proxy_redirect | Syntax: proxy_redirect default; proxy_redirect off; proxy_redirect redirect replacement; Default: proxy_redirect default; Context: http, server, location  | |
| proxy_request_buffering | Syntax: proxy_request_buffering on | off; Default: proxy_request_buffering on; Context: http, server, location This directive appeared in version 1.7.11.  | |
| proxy_send_lowat | Syntax: proxy_send_lowat size; Default: proxy_send_lowat 0; Context: http, server, location  | |
| proxy_send_timeout | Syntax: proxy_send_timeout time; Default: proxy_send_timeout 60s; Context: http, server, location  | |
| proxy_set_body | Syntax: proxy_set_body value; Default: — Context: http, server, location  | |
| proxy_set_header | Syntax: proxy_set_header field value; Default: proxy_set_header Host $proxy_host; proxy_set_header Connection close; Context: http, server, location  | |
| proxy_ssl_certificate | Syntax: proxy_ssl_certificate file; Default: — Context: http, server, location This directive appeared in version 1.7.8.  | |
| proxy_ssl_certificate_key | Syntax: proxy_ssl_certificate_key file; Default: — Context: http, server, location This directive appeared in version 1.7.8.  | |
| proxy_ssl_ciphers | Syntax: proxy_ssl_ciphers ciphers; Default: proxy_ssl_ciphers DEFAULT; Context: http, server, location This directive appeared in version 1.5.6.  | |
| proxy_ssl_crl | Syntax: proxy_ssl_crl file; Default: — Context: http, server, location This directive appeared in version 1.7.0.  | |
| proxy_ssl_name | Syntax: proxy_ssl_name name; Default: proxy_ssl_name $proxy_host; Context: http, server, location This directive appeared in version 1.7.0.  | |
| 。。。。。。 | 。。。。。。 | 。。。。。。。 | 
标签:nginx
原文地址:http://blog.51cto.com/13323775/2073514