nginx是什么?
nginx是一款轻量级、高性能的HTTP服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
nginx的优点:
1、占内存小,可以实现高并发连接,处理响应快;
2、可以实现HTTP服务器、虚拟主机、反向代理、负载均衡;
3、配置简单;
4、可以不暴露真实服务器的ip
nginx的启动与关闭
将nginx压缩包解压后,双击nginx.exe,或者使用命令 start nginx 就启动成功了
验证:http://localhost
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
这里说一下,nginx默认监听80端口
当第一次启动nginx时,输入localhost,访问地址就是http://127.0.0.1:80,页面跳转到上述Welcome to nginx!
响应的页面可以再nginx.conf文件中配置的
server {
listen 80;
server_name localhost;
location / {
root html;
index index2.html index2.htm;
}
}
所以欢迎页面可以自己设置,只要将页面放在静态资源的文件夹html中,并修改index即可。
(当然也可以不放在html文件夹中,只要修改root就行了,要使用相对路径)
关闭nginx:
1、cmd进入到nginx解压目录
输入命令: nginx -s stop
2、直接在任务管理器中关闭nginx进程。
有时候上面两个方法都关不掉,就干错简单粗暴一点,直接用dos命令关闭这个进程
taskkill /f /t /im nginx.exe
使用nginx实现反向代理
=====================================分割线========================================
记下一些可能很low的问题,但是我还真不会
将Tomcat解压后,运行bin文件夹下的startup.bat文件,黑窗口一闪而过,然后我百度,在文件最后加上pause,查看具体报错信息
the JRE_HOME environment variable is not defined correctly
解决办法:添加JRE_HOME环境变量
然后再启动,又报错
java.lang.UnsatisfiedLinkError:D:\apache-tomcat-7\tcnative-1.dll:can't load IA 64bit .dll on a AMD 64-bit platform
查看了JAVA_HOME和JRE_HOME以及PATH等环境变量,都不知道这个D:\apache-tomcat-7是在哪里配置的,后俩发现是CATALINA-HOME环境变量,删掉了以后就好了。
一、修改tomcat访问主页
webapps\ROOT文件夹内的东西可以全部删除,然后自己创建一个index.html文件即可。
二、修改tomcat启动端口
conf\server.xml文件
注意:改的不是一个端口,而是三个
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
========================================================================================
现在两个tomcat以不同的端口已经启动成功了,下面通过nignx实现反向代理
第一:配置域名(DNS解析)
C:\Windows\System32\drivers\etc\hosts
127.0.0.1 8080.itmayiedu.com
127.0.0.1 8081.itmayiedu.com
当在浏览器中输入域名8080.itmayiedu.com或者8081.itmayiedu.com,通过DNS解析到127.0.0.1,
但是还必须在域名后面添加端口号,然后使用指定端口的Tomcat服务器。
第二:配置nginx
conf\nginx.conf文件
server {
listen 80;
server_name 8080.itmayiedu.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
server {
listen 80;
server_name 8081.itmayiedu.com;
location / {
proxy_pass http://127.0.0.1:8081;
index index.html index.htm;
}
}
重启nginx,输入域名8080.itmayiedu.com,将反向代理到127.0.0.1:8080,同理,8081.itmayiedu.com,反向代理到127.0.0.1:8081
nginx解决浏览器跨域访问问题
跨域访问问题是使用ajax访问的域名跟浏览器访问网站的域名不一致的问题,可以使用nginx解决
假设问题情境:我们网站的域名是itmayiedu.com,部署在一台服务器上(假设是8080端口的一台服务器),而ajax要跨域访问到另一台服务器(假设是8081端口的另一台服务器)
配置如下:
server {
listen 80;
server_name itmayiedu.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
location /tomcat_8081/ {
proxy_pass http://127.0.0.1:8081/;
index index.html index.htm;
}
}
(注意:第二个location配置中的斜杠)
nginx加tomcat实现负载均衡
负载均衡:提供一种廉价、有效、透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
简单的说就是,将客户端的http请求,均分到所有的服务器中,不会让一个服务器因为请求太多而宕机,也不会让一个服务器请求太少而浪费。
第一:配置域名(DNS解析)
C:\Windows\System32\drivers\etc\hosts
127.0.0.1 www.itmayiedu.com
第二:配置nginx
conf\nginx.conf文件
#upstream 可以配置多个上游服务器(权重算法,默认轮询)
upstream nginxTestServer {
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name www.itmayiedu.com;
location / {
proxy_pass http://nginxTestServer;
index index.html index.htm;
}
}
当输入域名www.itmayiedu.com,将反向代理到nginxTestServer服务器,而nginxTestServer配置了两个tomcat服务器,权重各为1,效果就是两个服务器逐次轮询,实现负载均衡。
(注意浏览器缓存,我测试的时候IE浏览器有缓存,响应的请求一直不变,都是第一个服务器。)
nginx负载均衡故障转移
设置超时时间
server {
listen 80;
server_name www.itmayiedu.com;
location / {
proxy_pass http://nginxTestServer;
proxy_connect_timeout 1s;
proxy_send_timeout 1s;
proxy_read_timeout 1s;
index index.html index.htm;
}
}