Nginx实例
一、反向代理
反向代理实例一
1.实现效果
打开浏览器,在浏览器地址栏输入地址www.pluto.com,跳转到 liunx 系统 tomcat 主页面中
2.准备工作
[1].安装tomcat
[root@host79 sbin]# cd /opt/apache-tomcat-7.0.70/ [root@host79 apache-tomcat-7.0.70]# ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work [root@host79 apache-tomcat-7.0.70]# cd bin/ [root@host79 bin]# ./startup.sh Using CATALINA_BASE: /opt/apache-tomcat-7.0.70 Using CATALINA_HOME: /opt/apache-tomcat-7.0.70 Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.70/temp Using JRE_HOME: /opt/jdk1.7.0_79 Using CLASSPATH: /opt/apache-tomcat-7.0.70/bin/bootstrap.jar:/opt/apache-tomcat-7.0.70/bin/tomcat-juli.jar Tomcat started. [root@host79 bin]# cd .. [root@host79 apache-tomcat-7.0.70]# cd logs/ [root@host79 logs]# ls catalina.2020-07-20.log host-manager.2020-08-12.log localhost_access_log.2020-08-12.txt catalina.2020-07-29.log localhost.2020-07-20.log manager.2020-07-20.log catalina.2020-08-12.log localhost.2020-07-29.log manager.2020-08-12.log catalina.out localhost.2020-08-12.log host-manager.2020-07-20.log localhost_access_log.2020-07-20.txt [root@host79 logs]# tail -f catalina.out 八月 12, 2020 11:21:04 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deploying web application directory /opt/apache-tomcat-7.0.70/webapps/ROOT 八月 12, 2020 11:21:04 下午 org.apache.catalina.startup.HostConfig deployDirectory 信息: Deployment of web application directory /opt/apache-tomcat-7.0.70/webapps/ROOT has finished in 183 ms 八月 12, 2020 11:21:04 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-bio-8080"] 八月 12, 2020 11:21:04 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-bio-8009"] 八月 12, 2020 11:21:04 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 5663 ms |
[2].开放端口
[root@host79 /]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT [root@host79 /]# service iptables restart [root@host79 /]# /etc/init.d/iptables status |
3.具体配置
[1].windows系统修改hosts文件
在 windows 系统的 host 文件进行域名和 ip 对应关系的配置
#C:\Windows\System32\drivers\etc\hosts 192.168.188.188 www.pluto.com |
[2].nginx反向代理配置(请求转发配置)
[root@host79 /]# cd /usr/local/nginx/conf/ [root@host79 conf]# vim nginx.conf |
[3].测试
[root@host79 /]# cd /usr/local/nginx/sbin/ [root@host79 sbin]# ./nginx -s reload |
反向代理实例二
1.实现效果
使用 nginx 反向代理,根据访问的路径跳转到不同端口的服务中nginx 监听端口为 8001.
访问 http://192.168.188.188:8001/edu/ 直接跳转到 127.0.0.1:8080
访问 http://192.168.188.188:8001/vod/ 直接跳转到 127.0.0.1:8081
2.准备工作
[1].准备两个tomcat服务器,一个8080端口,一个8081端口
#8080tomcat [root@host79 bin]# pwd /opt/tomcat8080/apache-tomcat-7.0.70/bin [root@host79 bin]# ./start #8081tomcat [root@host79 conf]# pwd /opt/tomcat8081/apache-tomcat-7.0.70/conf [root@host79 conf]# vim server.xml <Server port="8015" shutdown="SHUTDOWN"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" /> |
[2].将端口添加到防火墙
[root@host79 conf]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT |
[3].测试页面
[4].创建文件夹
[root@host79 /]# cd /opt/tomcat8080/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir edu [root@host79 webapps]# cd edu [root@host79 edu]# vim a.html <h1>8080</h1> [root@host79 /]# cd /opt/tomcat8081/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir vod [root@host79 webapps]# cd vod [root@host79 webapps]# vim a.html <h1>8081</h1> |
3.具体配置
[1].nginx配置文件(反向代理配置)
[root@host79 conf]# pwd /usr/local/nginx/conf [root@host79 conf]# vim nginx.conf #通过以下命令查看防火墙是否放行8080 8081 8001端口 [root@host79 conf]# service iptables status [root@host79 sbin]# pwd /usr/local/nginx/sbin [root@host79 sbin]# ./nginx -s reload #若出现nginx: [emerg] invalid URL prefix in URL/nginx.conf,则配置nginx.conf时一定出错了 #若出现nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) ,则是https://www.cnblogs.com/houss/p/11291629.html |
[2].测试
二、负载均衡
1.实现效果
浏览器地址栏输入地址 http://192.168.188.188/edu/a.html,负载均衡效果,平均 8080和 8081 端口中
2.准备工作
[1].准备两个tomcat服务器,一个8080端口,一个8081端口
#8080tomcat [root@host79 bin]# pwd /opt/tomcat8080/apache-tomcat-7.0.70/bin [root@host79 bin]# ./start #8081tomcat [root@host79 conf]# pwd /opt/tomcat8081/apache-tomcat-7.0.70/conf [root@host79 conf]# vim server.xml <Server port="8015" shutdown="SHUTDOWN"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" /> |
[2].将端口添加到防火墙
[root@host79 conf]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT |
[3].创建文件
[root@host79 ~]# cd /opt/tomcat8080/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir pluto [root@host79 webapps]# cd pluto [root@host79 pluto]# vim a.html <h1>8080!!!!!!</h1> [root@host79 ~]# cd /opt/tomcat8081/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir pluto [root@host79 webapps]# cd pluto [root@host79 pluto]# vim a.html <h1>8081!!!!!!!!!</h1> |
[4].测试
3.nginx配置文件(负载均衡配置)
[root@host79 conf]# pwd /usr/local/nginx/conf [root@host79 conf]# vim nginx.conf #gzip on; upstream myserver{ server 192.168.188.188:8080; server 192.168.188.188:8081; } server { listen 80; server_name 192.168.188.188; location / { root html; proxy_pass http://myserver; index index.html index.htm; } |
4.nginx分配服务器策略
[1].轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
[2].weight
weight 代表权重默认为 1,权重越高被分配的客户端越多.weight 和访问比率成正比,用于后端服务器性能不均的情况。
[root@host79 conf]# pwd /usr/local/nginx/conf [root@host79 conf]# vim nginx.conf #gzip on; upstream myserver{ server 192.168.188.188:8080 weight=10; server 192.168.188.188:8081 weight=5; } server { listen 80; server_name 192.168.188.188; location / { root html; proxy_pass http://myserver; index index.html index.htm; } |
[3].ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题
upstream myserver{ ip_hash server 192.168.188.188:8080; server 192.168.188.188:8081; } |
[4].fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream myserver{ server 192.168.188.188:8080; server 192.168.188.188:8081; fair } |
三、动静分离
1.实现效果
2.准备工作
[root@host79 /]# mkdir data [root@host79 data]# mkdir www [root@host79 data]# mkdir image [root@host79 data]# ll 总用量 8 drwxr-xr-x. 2 root root 4096 8月 13 15:43 image drwxr-xr-x. 2 root root 4096 8月 13 15:49 www [root@host79 data]# cd www/ [root@host79 www]# vim a.html <h1>test html!!!</h1> [root@host79 data]# cd /data/image/ #上传一张图片 |
3.具体配置
[root@host79 /]# cd /usr/local/nginx/conf/ [root@host79 conf]# vim nginx.conf |
4.测试
注:因为nginx.conf中添加了autoindex on;所以会自动列出所有的文件信息
Nginx实例的更多相关文章
- Docker学习笔记--Docker 启动nginx实例挂载目录权限不够(转)
今天在启动一个docker 运行nginx实例,在挂载目录时,出现访问静态目录时,权限不够 执行的命令是: docker run --name my-nginx -d -p 80:80 --resta ...
- saltstack SLS 安装haproxy+nginx实例分析学习
本文主要以实例的形式去熟悉sls的部署流程及相关模块的使用 文件下载:https://github.com/unixhot/saltbook-code 目录结构 [root@k8s_master sa ...
- docker安装nginx实例
1.拉取nginx镜像: docker pull nginx 2.查看本地镜像文件: docker images 3.创建挂载目录: mkdir -p /docker_data/nginx/{con ...
- Nginx模块参考手册:HTTP核心模块
FROM: http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=17238776&id=2982697 这些模块默认会全部编 ...
- LINUX测试环境部署nginx(五)
安装配置nginx 安装编译环境:yum -y install pcre-devel openssl openssl-devel 拷贝nginx压缩文件到目标目录后,解压tar -zxvf nginx ...
- Nginx介绍
原文:http://www.aosabook.org/en/nginx.html 作者: Andrew Alexeev nginx(发音"engine x")是俄罗斯软件工程师Ig ...
- Nginx 的启动、停止、平滑重启、信号控制和平滑升级
Nginx 的启动 假设 nginx 安装在 /usr/local/nginx 目录中,那么启动 nginx 的命令就是: [root@localhost ~]# /usr/local ...
- Linux下Nginx的安装、升级及动态添加模块
系统基于ubuntu server 14.04.4 amd64 安装 第一步 下载并解压Nginx压缩包 从Nginx官网下载Nginx,或者在Linux上执行wget http://nginx.or ...
- nginx 1.安装
nginx 1.安装 nginx的众多优点这里就不多说了,直接开始吧. 基本依赖 yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel ...
随机推荐
- centos8使用systemctl管理运行级别
一,什么是systemd的target? 1,关于systemd/systemctl的相关知识,请移步到这一篇 https://www.cnblogs.com/architectforest/p/12 ...
- 从零开始针对 .NET 应用的 DevOps 运营实践 - Jenkins & SonarQube 安装配置
一.Overview 继续 DevOps 实施的相关内容,在上一篇的博客中,完成了对于工具链中使用到的软件所需的运行环境的配置,在这一篇的博客中,将聚焦于我们使用到的两个主要的软件:Jenkins 与 ...
- Gitlab 11.9.1 高可用教程
Gitlab 11.9.1 高可用教程 一. PostgreSQL数据迁移 由于默认Gitlab的安装会内置Postgres数据库,并且没有对外,所以我们需要通过设置对应的Gitlab的配置将其中的数 ...
- [转]CSS学习笔记
原文:http://www.fx114.net/qa-266-93710.aspx 01.什么是CSS. CSS指层叠样式表(Cascading Style Sheets). ·样式定义如 ...
- 第二十二章 Nginx性能优化
一.性能优化概述 1.我们需要了解 1.首先需要了解我们当前系统的结构和瓶颈,了解当前使用的是什么,运行的是什么业务,都有哪些服务,了解每个服务最大能支撑多少并发.比如nginx作为静态资源服务并发是 ...
- Linux显示系统信息sh脚本
#!/bin/bash # #******************************************************************** #Author: wangxia ...
- Android 限制控件多次点击
有时候多次点击页面会连续弹出多个页面,这时候写一个方法控制一下就OK. private static long lastClickTime; public synchronized static b ...
- tomcat 启动失败
1.tomcat单独通过脚本可以启动,但是http://localhost:8080加载页面失败 tomcat 启动失败: a.检查JAVA_HOME和CATALINA_HOME是否配置正确:或者直接 ...
- java面试之手写单例模式
为什么要有单例模式 实际编程应用场景中,有一些对象其实我们只需要一个,比如线程池对象.缓存.系统全局配置对象等.这样可以就保证一个在全局使用的类不被频繁地创建与销毁,节省系统资源. 实现单例模式的几个 ...
- jumpserver部署使用
一.简介 前面我们聊到了openvpn的部署和使用,它能够实现从互联网通过openvpn连接到公司内网服务器,从而进行远程管理:但openvpn有一个缺点它不能记录哪些用户在内网服务器上操作了什么,拥 ...