Nginx是一款轻量级Web服务器、反向代理服务器以及电子邮件代理服务器。反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
1、配置文件基本结构
event { 网络模型定义}http { 网站整体环境配置 server { 一个可以访问的web服务器 }}
2、配置文件详细介绍
user nobody; //Nginx的伪用户和伪用户组worker_processes 1; //启动进程,通常设置成和CUP的数量相等,相当于CPU的个数 #错误日志 error_log logs/error.log;error_log logs/error.log notice;error_log logs/error.log info;pid logs/nginx.pid; //主进程PID的保存文件events { use epoll; //网络I/O模型,建议Linux使用epoll,epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内 核,可以大大提高nginx的性能 #工作进程最大允许连接数 worker_connections 1024; }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; #请求日志保存位置 server_names_hash_bucket_size 128; #保存服务器名字的hash表大小 client_header_buffer_size 32k; #客户端请求头部缓冲区大小 large_client_header_buffers 432k; #最大客户端头缓冲大小 client_max_body_size 50m; #客户端最大上传文件大小(M) #sendfile指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网 #络I/O处理速度,降低系统的uptime. sendfile on; #这个是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。(只在sendfile on时有效) tcp_nopush on; keepalive_timeout 65; #链接超时时间 tcp_nodelay on; #禁用nagle算法,即不缓存数据 #Nginx的FastCGI模块设置 fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; #开启gzip网络压缩 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; server_tokens off; #隐藏nginx版本号(curl -I 192.168.4.154可以查看,更加安全) #第一个虚拟主机 server { listen 80; server_name localhost; #服务器名称 index index.html index.htm index.php #默认网页文件 root /webapp; #部署的web项目所在的路径 charset koi8-r; #设定字符集 access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #开启status状态监测 location /status { stub_status on; access_log off; } #静态文件缓存时间设置30天 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } #静态文件缓存时间设置12小时 location ~ .*\.(js|css)?$ { expires 12h; } #error_page 404 /404.html; #重定向服务器的错误页面到指定的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; #} } #让domain子目录下的配置文件生效 include domain/*.conf; }