一、安装Nginx
二、Nginx编译选项
三、Nginx进程管理命令
四、Nginx配置文件解析
一、安装Nginx
1、提前安装所需软件依赖包
yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel perl perl-ExtUtils-Embed
2、下载Nginx源码安装包
wget http://nginx.org/download/nginx-1.4.0.tar.gz
3、解压编译(禁用模块使用参数--without,编译第三方模块使用参数--add-module=/path/module)
tar -xzf nginx-1.4.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.4.0/ ./configure --prefix=/usr/local/nginx --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_perl_module --with-mail --with-mail_ssl_module
4、安装
make && make install
安装完成后,程序主目录位于/usr/local/nginx/,目录下的内容分别为:
conf,主配置文件目录
html,网站根目录
logs,日志文件目录
sbin,主程序目录
二、Nginx编译选项
1、默认自动编译项 禁用选项 Core:Nginx核心功能, --without-http Access:基于IP的访问控制 --without-http_access_module Auth Basic:HTTP用户认证模块 --without-http_auth_basic_module Auto Index:自动目录索引 --without-http_autoindex_module Browser:描述用户代理 --without-http_charset_module Charset:重新编码网页 --without-http_charset_module Empty GIF:内存中存放一个图片 --without-http_empty_gif_module FastCGI:FastCGI支持 --without-http_fastcgi_module Geo:支持IP变量设置 --without-http_geo_module Gzip:Gzip压缩 --without-http_gzip_module Limit Requests:限制客户端连接频率 --without-http_limit_req_module Limit Conn:挥发的并发连接 --without-http_limit_conn_module Map:设置变量 --without-http_map_module Memcached:Memcache支持 --without-http_memcached_module Referer:基于Referer头部信息过滤 --without-http_referer_module Rewrite:使用正则表达式重写请求 --without-http_rewrite_module SCGI:支持SCGI协议 --without-http_scgi_module Upstream:负载均衡 --without-http_upstream_ip_hash_module Headers:设置http响应的头部信息 Index:首页 Log:自定义日志
2、内置模块中的附加模块,需要在编译时手动开启 开启选项 Embedded Perl:支持Perl --with-http_perl_module FLV:支持Flash视频 --with-http_flv_module GeoIP:通过IP变量实现负载均衡 --with-http_geoip_module Google Perftools:支持谷歌的性能优化工具 --with-google_perftools_module Gzip Precompression:压缩静态文件 --with-http_gzip_static_module Image Filter:转换图形的过滤器 --with-http_image_filter_module MP4:支持MP4 --with-http_mp4_module Real IP:使用Nginx作为后端服务器 --with-http_realip_module Secure Link:使用密匙保护页面 --with-http_secure_link_module SSL:支持HTTPS/SSL --with-http_ssl_module Stub Status:查看服务器状态 --with-http_stub_status_module WebDAV:支持WebDAV --with-http_dav_module ------------------------------------------ Core:邮件代理功能 --with-mail Core:邮件代理功能 --without-mail_pop3_module Core:邮件代理功能 --without-mail_imap_module Core:邮件代理功能 --without-mail_smtp_module ------------------------------------------ SSL:支持SSL/TLS加密邮件协议 --with-mail_ssl_module
三、Nginx进程管理命令
/usr/local/nginx/sbin/nginx #启动主程序 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #指定配置文件启动主程序 /usr/local/nginx/sbin/nginx -s stop #关闭主程序 /usr/local/nginx/sbin/nginx -s reload #重新加载设置
Nginx会将进程号保存在/usr/local/nginx/logs/nginx.pid文件中,可以使用kill指令发送信号给该进程号平滑重启Nginx
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
四、Nginx配置文件解析
Nginx默认配置文件为/usr/local/nginx/conf/nginx.conf,其内容如下
#user nobody; #设置用户与组
worker_processes 1; #启动子进程数,可以通过ps aux | grep nginx查看
#错误日志文件及日志级别notice,info
#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; #每个进程可以处理的连接数,受系统文件句柄限制,ulimit
}
http {
include mime.types; #mime.types为文件类型定义文件
default_type application/octet-stream; #默认文件类型
#log_format自定义入职格式,名称为main
#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; #创建访问日志,采用main定义的格式
sendfile on; #是否调用sendfile() 进行数据复制,sendfile复制数据是在内核级别完成的,会比一般的read、write更高效
#tcp_nopush on;
#keepalive_timeout 0; #保持连接的超时时间
keepalive_timeout 65;
#gzip on; #是否采用压缩功能,将页面压缩后传输更节省流量
#使用server定义虚拟主机
server {
listen 80; #服务器监听的端口
server_name localhost; #访问域名
#charset koi8-r; #编码格式,如果网页编码与此设置不同,则将被自动转码
#access_log logs/host.access.log main; #设置虚拟主机的访问日志路径及格式
#对URL进行匹配
location / {
root html; #root表示网页根路径,使用的是相对路径,html在nginx安装路径下
index index.html index.htm; #定义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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#访问以php结尾的页面,则自动将该请求转至127.0.0.1:80,通过proxy_pass可以实现代理功能
#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
#
#拒绝所有人访问.ht页面
#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; #监听TLS使用的
# server_name localhost;
# ssl on; #开启ssl功能
# ssl_certificate cert.pem; #指定证书文件,相对路径,文件需放在niginx.conf同目录下
# ssl_certificate_key cert.key; #指定私钥文件,相对路径,文件需放在niginx.conf同目录下
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}原文:http://64314491.blog.51cto.com/2784219/1672105