从nginx官网下载安装包 nginx-1.9.12.tar.gz
下载地址 http://nginx.org/
安装所需基础库
yum install pcre-devel
yum install zlib-devel
编译安装nginx
./configure –prefix=/usr/local/nginx
make
make install
查看nginx版本和安装参数
./sbin/nginx -V
conf/nginx.conf
# 使用的用户和组
#user nobody;
# 指定工作进程数
worker_processes 1;
# 错误日志保存位置
# 可以使用 [ debug | info | notice | warn | error | crit ] 参数
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定 pid 存放的路径
#pid logs/nginx.pid;
# 事件模块
events {
# 默认使用哪种事件模型,即nginx处理连接的方法
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll | eventport ]
use epoll;
#每个worker的最大连接数
worker_connections 1024;
}
# HTTP模块
http {
# 不同文件后缀对应的MIME
include mime.types;
# 默认MIME类型为二进制字节流
default_type application/octet-stream;
#charset gb2312
# 日志输出格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "request_time" "$http_x_forwarded_for"';
# 日志默认输出路径和格式
#access_log logs/access.log main;
#server_names_hash_bucket_size 128;
#client_header_buffer_size 32k;
#large_client_header_buffers 4 64k;
#client_max_body_size 100m;
#send_timeout 120;
# 开启调用Linux的sendfile(),提供文件传输效率
sendfile on;
# 是否允许使用socket的TCP_NOPUSH或TCP_CORK选项
#tcp_nopush on;
# 指定客户端连接保持活动的超时时间,在这个时间之后,服务器会关掉连接
#keepalive_timeout 0;
keepalive_timeout 65;
#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 128k;
#fastcgi_intercept_errors on;
# 设置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;
# 设定负载均衡的服务器列表
#upstream myproject {
# weigth参数表示权值,权值越高被分配到的几率越大
# max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查
# fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器
#server 192.168.1.2:8080 weight=5 max_fails=2 fail_timeout=600s;
#server 192.168.1.3:8080 weight=3 max_fails=2 fail_timeout=600s;
#}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# 反向代理的配置.
# proxy_redirect off;
# 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服务器传
# 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;
#}
# 设定查看Nginx状态的地址
#location /NginxStatus {
#stub_status on;
#access_log off;
#allow 192.168.0.2;
#deny all;
#auth_basic "NginxStatus";
#auth_basic_user_file conf/htpasswd;
#}
}
# 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;
# }
#}
}
HTTP模块的核心变量
三个作用域:http, server, location
server
语法:server {...}
作用域: http
配置一台虚拟机。
location
语法: location [=|~|~*|^~] /uri/ { ... }
作用域: server
配置访问路径的处理方法。
listen
语法: listen address:port [ default [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ]
默认值: listen 80
作用域: server
指定当前虚拟机的监听端口。
alias
语法: alias file-path|directory-path;
作用域: location
该指令设置指定location使用的路径.注意它跟 root 相似,但是不改变文件的根路径,仅仅是使用文件系统路径
root
语法: root path
默认值:root html
作用域:http, server, location
alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录。
区别:
location /abc/ {
alias /home/html/abc/;
}
在这段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。这段配置亦可改成
location /abc/ {
root /home/html/;
}
这样,nginx就会去找/home/html/目录下的abc目录了,得到的结果是相同的。
HTTP模块的其他基本组件将结合案例介绍。
变量:
HTTP header 里边 特定HEADER的值,变量会转成小写,比如 $http_user_agent, $http_referer... header信息 "YOUR-STRANGE-HEADER: values" 能通过 $http_your_strange_header获得.
$arg_PARAMETER
$http_HEADER
$query_string = $args
启动nginx
./sbin/nginx //启动
./sbin/nginx -t //检查nginx.conf格式是否正确
./sbin/nginx -s restart //重加载nginx.conf