1.nginx简介
“nginx是一款轻量级的HTTP服务器,采用事件驱动的异步非阻塞处理方式框架,这让其具有极好的IO性能,时常用于服务端的反向代理和负载均衡。”
2.nginx优点
2.1 支持海量高并发:采用IO多路复用epoll。官方测试nginx能够支持5万并发链接,实际生产环境中可以支撑2-4万并发连接数。
2.2 内存消耗少:在主流的服务器中nginx目前是内存消耗最小的了,比如我们用nginx+PHP,在3万并发链接下,开启10个nginx进程消耗150M内存。
2.3 免费使用可以商业化:nginx为开源软件,采用的是2-clause BSD-like协议,可以免费使用,并且可以用于商业。并且配置简单
3.nginx配置介绍
nginx.conf
文件是nginx总配置文件,nginx的基本配置或者设置代理等等基本配置可以在此设置
#运行用户,默认即是nginx,可以不进行设置
user nginx;
#nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 单个后台进程的最大并发数
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
nginx.default.conf
,nginx具体的默认配置,代理,服务配置等等都可以写在这个配置文件里
server {
listen 80; #配置监听端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/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;
#}
}
4.nginx基本的访问权限配置
依靠deny和allow这两个指令,可以实现基本的访问权限控制,如以下的配置,表示只允许45.76.202.231
进行访问,其他的IP是禁止访问的
location / {
allow 45.76.202.231;
deny all;
}
如果对于其他复杂的需求,可以使用正则实现。
5.nginx设置虚拟主机
虚拟主机是指在一台物理主机服务器上划分出多个磁盘空间,每个磁盘空间都是一个虚拟主机,每台虚拟主机都可以对外提供Web服务,并且互不干扰。在外界看来,虚拟主机就是一台独立的服务器主机,这意味着用户能够利用虚拟主机把多个不同域名的网站部署在同一台服务器上,而不必再为简历一个网站单独购买一台服务器,既解决了维护服务器技术的难题,同时又极大地节省了服务器硬件成本和相关的维护费用。
具体做法是,在nginx.default.conf
添加多个server,可根据ip,端口或域名来划分,这样,就可以访问同一台服务器上的不同资源了,值得注意是,如果是使用域名,这还要让域名的dns解析指向你的服务器
server{
listen 8001;
server_name localhost;
root /usr/share/nginx/html/html8001;
index index.html;
}
server{
listen 8002;
server_name localhost;
root /usr/share/nginx/html/html8002;
index index.html;
}
6.nginx设置反向代理
客户端发送的请求,想要访问server服务器上的内容。发送的内容被发送到代理服务器上,这个代理服务器再把请求发送到自己设置好的内部服务器上,而用户真实想获得的内容就在这些设置好的服务器上,反向代理实际生产环境可以说用的非常多
server{
listen 80;
server_name https://www.baidu.com;
location / {
proxy_pass https://www.baidu1.com;
}
}
这样,当你访问https://www.baidu.com
的时候就会去请求https://www.baidu1.com
的资源了,以下还有常用的反向代理命令
proxy_set_header :在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息。
proxy_connect_timeout:配置nginx与后端代理服务器尝试建立连接的超时时间。
proxy_read_timeout : 配置nginx向后端服务器组发出read请求后,等待相应的超时时间。
proxy_send_timeout:配置nginx向后端服务器组发出write请求后,等待相应的超时时间。
proxy_redirect :用于修改后端服务器返回的响应头中的Location和Refresh
7.nginx匹配PC或wap端
server{
listen 80;
server_name nginx2.jspang.com;
location / {
root /usr/share/nginx/pc;
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/mobile;
}
index index.html;
}
}