一、nginx简介
1.nginx的发展
Nginx是俄罗斯人编写的一款高性能HTTP和反向代理服务器。Nginx能够选择高效的epoll(Linux2.6内核)、kqueue(FreeBSD)、eventport(Solaris 10)作为网络I/O模型,再高连接并发的场景下,Nginx是Apache服务器非常不错的替代品,它能够支持50000个并发连接数的响应,而CPU、内存等系统资源消耗却非常低,运行非常稳定。
2.为什么选择Nginx
2.1 它可以高并发连接
官方测试Nginx可以支持5w并发连接,在实际生产环境中可以支持2~4w并发连接数。这得益于Nginx使用了最新的epoll和kqueue网络I/O模型,而Apache则使用的老的select模型。
2.2 内存消耗少
2.3 成本低廉
3.Nginx和Apache、Lighttpd的综合对比
二、Nginx的基本配置和优化
#使用的用户和组 #user nobody; #指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,例如两个4核CPU,则总核数为8) worker_processes 1; #错误日志存放路径 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定PID存放路径 #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; include https_params.conf; server { listen 8888; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location /test { proxy_pass https://www.baidu.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Server-IP $server_name; proxy_set_header X-Server-Port $server_port; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 30; proxy_send_timeout 30; proxy_read_timeout 300; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } # 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; # } #} }
上面是Nginx的nginx.conf配置文件,可见配置文件的构成如下:
....
events {
....
}
....
http {
....
server {
....
}
server {
....
}
....
}
1.Nginx虚拟主机配置
1.1 什么是虚拟主机
其实就是把一台运行在互联网上的服务器划分成多个“虚拟”的服务器,并且每一台虚拟主机都具有独立的域名和完整的Internet服务器功能。同一台服务器上的不同虚拟主机是各自独立的,可由客户自行管理。不过一台服务器只可以支持一定数量的虚拟主机,如果超出这个数量,那么客户在使用时将会发现性能急速降低。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。如下是一个虚拟主机的代码:
server { listen 8000; server_name somename alias another.alias; location / { root html; index index.html index.htm; } }
1.2 配置基于IP的虚拟主机
Linux、FreeBSD操作系统都允许添加ip别名。IP别名背后的概念很简单:可以在一块物理网卡上绑定多个ip地址。这样就可以在单一网卡的同一个服务器上运行多个基于ip的虚拟主机,nginx多ip虚拟主机配置如下:
http{ #第一个虚拟主机 server { listen 192.168.1.1:8000; server_name somename alias another.alias; location / { root html; index index.html index.htm; } } #第二个虚拟主机 server { listen 192.168.1.2:8000; server_name somename alias another.alias; location / { root html; index index.html index.htm; } } }
1.3 配置基于多域名的虚拟主机
基于域名的虚拟主机是最常见的虚拟主机。只需配置你的DNS服务器,将每个主机名映射到正确的ip地址,然后配置Nginx服务器,令其识别不同的主机名就可以了。Nginx配置如下:
http{ #第一个虚拟主机 server { listen 8000; server_name www.baidu.com; location / { root html; index index.html index.htm; } } #第二个虚拟主机 server { listen 8000; server_name www.weibo.com; location / { root html; index index.html index.htm; } } }