1.nginx特点
1)C语言,开源
2)高性能的反向代理工具,负载均衡,能解决跨域问题
3)多进程,启动后会产生一个master进程和多个工作进程,不需要考虑多线程并发问题
4)功能模块化,代码模块化,与http相关的代码在源码中,其他的在插件中,可进行二次开发
5)采用IO多路复用机制,无阻塞,性能高
6)支持sendfile文件传输方式,静态文件传输效率高,内核直接到内核(无需内核到用户空间,用户到内核),0拷贝文件传输
2.nginx配置文件说明
nginx配置文件分为四部分:main(全局设置),server(主机设置),upstream(负载均衡服务器配置),location(url匹配特定位置的设置)
#指定nginx worker进程运行用户及用户组,默认nobody
#user nobody;
#启动进程,通常设置成与CPU数量相等
worker_processes 1;
#定义全局错误日志文件,级别有:debug、info、notice、warn、error和crit。debug输出日志最为详细,#criti输出日志最少。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定进程id的存储文件位置
#pid logs/nginx.pid;
#设定工作模式及连接数上限
events {
#支持的工作模式有:select,poll,kqueue,epoll,rtsig,linux首选epoll.
#设置epoll多路复用IO方式,但是仅用于linux2.6以上内核,可以大大提高nginx性能
#use epoll;
#每个后台工作进程的最大并发连接数
worker_connections 1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,由mime.type文件定义
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;
#指定nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设置为on,
#用于开启高效文件传输模式,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与
#网络I/O处理速度,降低系统的uptime
sendfile on;
#将tcp_nopush和tcp_nodelay两个指令设置为on,用于防止网络阻塞
#tcp_nopush on;
#tcp_nodelay on;
#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩,提高传输速度
#gzip on;
#设定请求缓冲
#client_header_buffer_size 1k;
#large_client_header_buffers 4 4k;
#包含的配置文件
#include /etc/nginx/conf.d/*.conf;
#设定负载均衡的服务器列表
#upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
#server 192.168.8.1:3128 weight=5;
#server 192.168.8.2:80 weight=1;
#server 192.168.8.3:80 weight=6;
#}
server {
#监听80端口
listen 80;
#定义使用localhost作为域名访问
server_name localhost;
#web服务器的语言编码
#charset koi8-r;
#设置本虚拟机主机的独立访问日志
#access_log logs/host.access.log main;
#默认请求,匹配url地址中有"/",则执行花括号中的配置
location / {
#定义服务器的默认网站根路径位置,虚拟主机的本地目录,完整路径:/opt/nginx/html,也
#可写绝对路径
root html;
#默认首页索引文件的格式及顺序
index index.html index.htm;
}
#定义错误提示页面
#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;
}
##php脚本请求转发到本地
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#php脚本请求全部转发到FastCGI处理,使用FastCGI默认配置
# 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;
#}
#禁止访问.ht***文件
# 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;
# }
#}
#虚拟服务器
server {
#侦听192.168.8.x的80端口
listen 80;
server_name 192.168.8.x;
#对aspx后缀的进行负载均衡请求
location ~ .*\.aspx$ {
root /root;#定义服务器的默认网站根目录位置
index index.php index.html index.htm;#定义首页索引文件的名称
proxy_pass http://mysvr;#请求转向mysvr 定义的服务器列表
#以下是一些反向代理的配置可删除.
proxy_redirect off;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
}
}
参考:
【Nginx反向代理以及负载均衡配置】https://www.cnblogs.com/Miss-mickey/p/6734831.html
【Nginx源码分析(25篇)】https://blog.csdn.net/yangyin007/article/details/82777086
【nginx介绍及特点】https://blog.csdn.net/fengdijiang/article/details/93993546
【详解Nginx配置文件结构】https://blog.51cto.com/gupt12/1322969