1.基本的配置
# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建虚拟主机(主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; location / { # 2.把所有对 www.liujun.com:8090 的请求转发到 https://www.taobao.com proxy_pass https://www.taobao.com; } } }这种反向代理可以实现 把 HTTP 转换成更安全的 HTTPS 的方案
2.其它配置
代理其他的服务器
# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建虚拟主机(主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; location / { # 2.把所有对 www.liujun.com:8090 的请求转发到 https://news-at.zhihu.com proxy_pass https://news-at.zhihu.com; # 3.其它配置 proxy_redirect off; # off使 location 或者 refresh 字段维持不变 # 默认情况下反向代理是不会转发请求中的 Host 头部的。如果需要转发,那么必须加上下面的配置 proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }浏览器访问:http://www.liujun.com:8090/api/4/news/latest 反向代理后变成请求 https://news-at.zhihu.com/api/4/news/latest,最终获取到结果.
地址栏没有变还是http://www.liujun.com:8090/api/4/news/latest
代理同一台服务器的同一个端口
# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.创建虚拟主机(主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; location / { # 2.把所有对 www.liujun.com:8090 的请求转发到 www.new.liujun.com:8090 proxy_pass http://www.new.liujun.com:8090; # 3.其它配置 proxy_redirect off; # off使 location 或者 refresh 字段维持不变 # 默认情况下反向代理是不会转发请求中的 Host 头部的。如果需要转发,那么必须加上下面的配置 # 参考文章:https://blog.csdn.net/felix_yujing/article/details/51682655 proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } # 2.创建第二个虚拟主机(虚拟主机名称为:www.new.liujun.com) server { listen 8090; server_name www.new.liujun.com; # 本地测试要在hosts中添加对 www.new.liujun.com 的解析 location / { root /usr/local/var/www/webapps/new; # (new项目中只有一个index.html) index index.html index.htm; } } }浏览器访问:http://www.liujun.com:8090/ 显示的是new项目的网页
地址栏没有变还是http://www.liujun.com:8090/
1.动态资源 :api接口 、文件 、压缩包 、软件 、 .jsp 、.php 、.shtml 、.ejs 等等
2.静态资源:html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css
# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 创建虚拟主机(主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; # 1.动态资源的加载。 location / { # 把所有对 www.liujun.com:8090 的动态资源的请求转发到 https://news-at.zhihu.com proxy_pass https://news-at.zhihu.com; # 其它配置 proxy_redirect off; proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 2.静态资源的加载。匹配以.html 、.htm 、.gif、.jpg、.jpeg结尾的请求 # ( ~ 表示匹配 URI 时是字母大小写敏感的; ~* 大小写不敏感; \. 是转译.) location ~* \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /usr/local/var/www/webapps/new; # (new项目中只有一个index.html) # expires定义用户浏览器缓存的时间为3天,如静态页不常更新,可以设更长 expires 30d; } } }上面两个 location 配置快是并列关系
1)浏览器访问:http://www.liujun.com:8090 反向代理到:https://news-at.zhihu.com 这个网站( 地址栏不变 )
2)浏览器访问:http://www.liujun.com:8090/api/4/news/latest 反向代理到 :https://news-at.zhihu.com/api/4/news/latest 这个api 接口
3)浏览器访问:http://www.liujun.com:8090/index.html 直接获取本地的服务器 /usr/local/var/www/webapps/new; 路径下的静态资源。
由于 Nginx 具有“强悍”的高并发高负载能力,因此一般会作为前端的服务器直接向客户端提供静态文件服务。但也有一些复杂、多变的业务不适合放到 Nginx 服务器上,这时会用Apache、Tomcat 等服务器来处理。于是Nginx 通常会被配置为既是静态 Web 服务器也是反向代理服务器,不适合 Nginx 处理的请求就会直接转发到上游服务器中处理。而upstream 块定义了一个上游服务器的集群,便于反向代理中的 proxy_pass 使用。
# 修改配置文件需要重启服务器 user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; #4.负载均衡配置。负载均衡是指选择一种策略,尽量把请求平均地分布到每一台上游服务器上 # weight: 设置向这台上游服务器转发的权重,默认为 1; # max_fails: 与 fail_timeout 配合使用,在fail_timeout时间内检查max_fails次数,失败则剔除均衡 # fail_timeout: 表示该时间段内转发失败多少次后就认为上游服务器暂时不可用,用于优化反向代理功能 upstream backend_api_service{ # backend_api_service 是负载均衡的名称 server www.liujun.com:8888 weight=1 max_fails=2 fail_timeout=30s; server www.liujun.com:9999 weight=1 max_fails=2 fail_timeout=30s; } # 1.创建虚拟主机(主机名称为:www.liujun.com) server { listen 8090; server_name www.liujun.com; location / { # 4.把所有对 www.liujun.com:8090 动态资源的请求转发到 http://backend_api_service proxy_pass http://backend_api_service; # backend_api_service 是负载均衡的名称 # 5.其它配置 # 如果后端的服务器返回502,504执行超时等错误,自动将请求转发到upstream负载均衡池 # 中的另一台服务器,实现故障转移 proxy_next_upstream http_502 http_504 error timeout invalid_header; } location ~* \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /usr/local/var/www/webapps/blog; # (blog项目中只有一个index.html) expires 30d; } } # 2.创建虚拟主机(主机名称为:www.liujun.com) server { listen 8888; server_name www.liujun.com; location / { root /usr/local/var/www/webapps/new; # 2.修改发布项目的目录 index index.html index.htm; } } # 3.创建虚拟主机(主机名称为:www.liujun.com) server { listen 9999; server_name www.liujun.com; location / { root /usr/local/var/www/webapps/music; # 2.修改发布项目的目录 index index.html index.htm; } } }1)浏览器访问:http://www.liujun.com:8090/index.html 访问的blog项目
2)浏览器访问:http://www.liujun.com:8090 访问的是new 项目 或者 是music项目
真实环境中负载均衡backend_api_service里面配的都是同一个项目发布在不同的服务器上
http://nginx.org/en/docs/http/ngx_http_core_module.html
user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.指定nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通的应用必须为on # 如果用来进行下载等应用磁盘IO负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime sendfile on; # 2.在打开 sendfile 选项时,确定是否开启 FreeBSD 系统上的 TCP_NOPUSH 或 Linux 系统上 # 的 TCP_CORK 功能。打开 tcp_nopush 后,将会在发送响应时把整个响应包头放到一个 TCP 包中发送 # ( 防止网络阻塞 ) tcp_nopush on; # 3.开始目录列表访问,适合下载服务器(例如:镜像下载网站),默认关闭 autoindex on; # 4.提高数据的实时响应性 tcp_nodelay on; # 5.打开文件缓存( 这个将为打开文件制定缓存,默认是没有启用的,max制定缓存数量,建议和打开文件数一致 # , inactive 是指经过多长时间文件没有被请求后删除缓存。) open_file_cache max=102400 inactive=20s; # 6.检验缓存中元素有效性的频率, 默认60s( 这个是指定多长时间检查一次缓存有效信息 ) open_file_cache_valid 30s; # 7.不被淘汰的最小访问次数( open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果 # 超过这个数字,文件描述符一直是在缓存中打开的) open_file_cache_min_uses 1; server { listen 9090; server_name localhost; location / { root /usr/local/var/www/webapps/blog; # 2.修改发布项目的目录 index index.html index.htm; } } }http://nginx.org/en/docs/http/ngx_http_gzip_module.html
user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.启动gzip压缩( 可以对css , js, image静态资源压缩) gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; # 2.压缩级别大小,最大为9,最小为1,压缩后比例越小越好,cpu处理更快(例如下面:100k-> 80k) # 设值的值越大,消耗cpu比较高(例如设置 9 :100k-> 10k ) gzip_comp_level 2; server { listen 9090; server_name localhost; location / { root /usr/local/var/www/webapps/new; # 2.修改发布项目的目录 index index.html index.htm; } } }http://nginx.org/en/docs/http/ngx_http_core_module.html
user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.HTTP 请求包体的最大值。允许客户端请求的最大单个文件字节数(php后端用的多) client_max_body_size 10m; # 2.存储 HTTP 包体的内存 buffer 大小( 缓存区代理缓冲用户端请求的最大字节数 ) client_body_buffer_size 128k; # 3.存储超大 HTTP 头部的内存 buffer 大小(设定请求缓冲) large_client_header_buffers 4 4k; # 4.存储 HTTP 头部的内存 buffer 大小 # 客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k # 不过由于一般系统分页都要大于1k, 所以这里设置分页大小为4k client_header_buffer_size 4k; server { listen 9090; server_name localhost; location / { root /usr/local/var/www/webapps/new; # 2.修改发布项目的目录 index index.html index.htm; } } }http://nginx.org/en/docs/http/ngx_http_proxy_module.html
user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; # 1.nginx跟后端服务器连接超时时间( 代理连接超时 ) proxy_connect_timeout 90; # 2.后端服务器数据回传时间(代理发送超时) proxy_send_timeout 90; # 3.连接成功后,后端服务器响应时间(代理接受超时) proxy_read_timeout 90; # 4.设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffer_size 4k; # 5.proxy_buffers 缓冲区,网页平均在32k一下的话,着用设置 proxy_buffers 4 32k; # 6.高负荷下缓冲大小( 一般等于=proxy_buffer * 2 ) proxy_busy_buffers_size 64k; server { listen 9090; server_name localhost; location / { root /usr/local/var/www/webapps/new; # 2.修改发布项目的目录 index index.html index.htm; } } }http://nginx.org/en/docs/http/ngx_http_core_module.html
user liujun liu; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; server { listen 9090; server_name localhost; # 1.使用的字符集 #charset koi8-r; location / { root /usr/local/var/www/webapps/new; # 2.修改发布项目的目录 index index.html index.htm; } # 2.错误码404跳转的页面 error_page 404 /404.html; # 3.定义错误提示页面(错误码404跳转的页面) error_page 500 502 503 504 /50x.html; location = /50x.html { # 5.只有当用户请求是 /50x.html 时,才会使用该 location 下的配置 root html; } # 6.设定查看Nginx状态的地址:http://localhost:9090/NginxStatus location /NginxStatus { stub_status on; } } }