Web服务器分2类:
- web应用服务器:tomcat、resin、jetty
- web服务器:Apache服务器、Nginx、llS(window server)
- 区分:web服务器不能解析JSP等页面、只能处理js、css、html等静态资源
- 并发:web服务器的并发能力远高于web应用服务器(tomcat:200并发)
概述
- Nginx是一款高性能的HTTP服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
- 官方测试Nginx能够支撑5万并发链接,并且CPU、内存等资源消耗却非常低,运行非常稳定
- 启动非常容易,修改配置文件也不需要重启。几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。
角色
- 作为Web服务器:相比Apache、llS。使用更少资源、更高并发5万
- 作为负载均衡服务器:既可以在内部直接支持Rails和PHP,也可以支持作为HTTP代理服务器对外进行服务。Nginx是用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好得多
- 作为邮件代理服务器
应用场景
- http服务器。Nginx是一个http服务,可以独立提供http服务。可以做网页静态服务器
- 但是我们现在大部分的项目都是动态网站,动态站点不能部署到Nginx里,但是可以部署到tomcat服务器上,再通过Nginx做反向代理搭集群
- 但是我们经常会页面静态化,会把动态资源转成静态页面部署,可以大大节省成本
- 虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
- 反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用Nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。
网关功能
nginx可以作为web服务器,但更多时候,我们把它作为网关,因为它具备网关必备的功能:
- 反向代理
- 负载均衡
- 动态路由
- 请求过滤
天猫用的web服务器是Tengine(Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。)
nginx的使用
开启nginx:
- start nginx
- nginx有两个进程:主进程和工作进程
修改工作进程的nginx.conf配置文件
- 查看nginx是否监听了80端口
- netstat -aon|findstr “80”
- tasklist|findstr “12776”
- 0.0.0.0:80(外网访问)的80端口的确被Nginx占用,
- 但是127.0.0.1:80(内网访问)的80端口被System占用
- 前面提到的llS会占用80端口,
- 使用命令 netsh http show servicestate 查看一下当前的http服务状态,发现果然有问题,如下图所示:
- 查看任务管理器,可以看到是SQL Server Reporting Services
- 停止掉这个进程,并改为手动启用,重新加载Nginx,便可以访问
- 可能还有其他原因,请看【转载】
- https://www.cnblogs.com/xwgcxk/p/11819018.html
重新加载nginx
- nginx -s reload
nginx -s stop:停止nginx
nginx.conf配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name api.fly.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:10010;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
server {
listen 80;
server_name manage.fly.com;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
proxy_pass http://127.0.0.1:9001;
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}