nginx能做什么
- 反向代理
- 负载均衡 RR(默认) 权重 ip_hash fair url_hash
- http服务器
- 正向代理
反向代理(ReverseProxy)就是通过代理服务器来 接受internet的连接请求 然后将请求转发给内部网络上的服务器,并将服务器上得到的结果返回给请求客户端
|
|
常用实例 采用nginx反向代理来解决跨域问题
|
|
这样 更改请求地址为 /api
虚拟的路径和 访问的静态页面就处于同源了
可以使用这样的来处理跨域问题
负载均衡
默认按照请求顺序分配到不同的后端服务器 如果后端服务器down掉 能自己剔除
简单配置如下:
|
|
HTTP 服务器
eg1: 静态资源服务器
动静分离
这样的话 我们可以把html 等静态资源放在文件目录下 动态的话可以 根据 localtion后面的正则 匹配到代理服务器上
linux
nginx -s reload
window 热重启
nginx.exe -s reload
代码示例 12308 测试环境
nginx.conf base 通过载入
/nginx_vhos/
目录下的所有文件
include /usr/local/nginx/conf/nginx_vhost/*.conf
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 [dubbo@hd-node2 conf]$ vi nginx.confkeepalive_timeout 65;server_tokens off;tcp_nopush on;gzip on;gzip_comp_level 5;gzip_min_length 1k;gzip_buffers 4 16k;gzip_disable "MSIE [1-6]\.(?!.*SV1)";gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript application/json;gzip_http_version 1.0;gzip_vary on;log_format main '$remote_addr $http_x_forwarded_for [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$request_time" $request_body "$upstream_response_time"';access_log on;server_names_hash_max_size 4096;}http {include /usr/local/nginx/conf/mime.types;default_type application/octet-stream;sendfile on;output_buffers 1 128k;log_not_found off;keepalive_timeout 65;server_tokens off;tcp_nopush on;gzip on;gzip_comp_level 5;gzip_min_length 1k;gzip_buffers 4 16k;gzip_disable "MSIE [1-6]\.(?!.*SV1)";gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript application/json;gzip_http_version 1.0;gzip_vary on;log_format main '$remote_addr $http_x_forwarded_for [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$request_time" $request_body "$upstream_response_time"';access_log on;server_names_hash_max_size 4096;server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 100m;proxy_connect_timeout 5;proxy_read_timeout 60;proxy_send_timeout 5;proxy_buffer_size 16k;proxy_buffers 4 64k;proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;proxy_hide_header Vary;proxy_set_header Accept-Encoding ”;proxy_set_header Host $host;proxy_set_header Referer $http_referer;proxy_set_header Cookie $http_cookie;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;include /usr/local/nginx/conf/nginx_vhost/*.conf;}