配置文件: /usr/local/nginx/conf/nginx.conf
基本模块
核心模块 放于配置文件开始位置
daemon
on|off 启用或禁用守护进程模式(后台启动)
error_log logs/
error.
log
log_not_found
on|off 开启或禁用
404错误
master_process
on|off 一个主进程和worker进程
user username groupname worker进程使用配置
worker_priority 定义worker优先级,-
20~
19,默认
0
worker_processes
4; worker进程数量
事件模块
events{
worker_connections
1024;
accept_mutex
on; //使用一个接受互斥锁来打开套接字监听
accept_mutex_delay
500ms;
}
配置模块
include mime.types
http模块,提供web服务
http {
gzip
on;
server {
server_name www.mysite.com;
listen
80;
location
/html/ {
gzip
off;
}
}
server {}
}
server : 声明站点
server {
server_name www.mysite.com mysite.com;
listen
127.0.
0.
1:8080;
root /home/mysite.com/html;
index index.php index.html;
error_page
404 /notFound.html;
error_page
403 http:/
/www.mysite.com/;
error_page
500 501 502 503 504 /server_error.html;
log_not_found off;
recursive_error_pages off;
direction
5m;
keepalive_requests
100;
keepalive_timeout
60;
send_timeout
60;
include mime.types;
location /downloads/ {
internal;
types {
text/html html;
}
default_type application/octet-stream;
}
location /root/ {
limit_except
GET {
allow
192.168.
0.
1/
24;
deny all;
}
}
location /home/ {
alias /home/mysite.com/static/;
}
location
@rename {
try_files
$uri $uri.php
$uri.html
@error_location;
}
location
@error_location {
proxy_pass
127.0.
0.
1:8888;
}
}
location : 定义配置应用于网站特定目录,比如http全局开启gzip压缩,可以在html目录下关闭gzip压缩
server {
server_name mysite.com;
location /html/ {
}
}
location = /html {
}
location /html {
}
location ~ ^/html$ {
}
location ~* ^/html$ {
}
location @rename {
}
rewrite模块 (rewrite if)
正则表达式
元字符描述量词描述
^开始*0或多次$结束+1或多次.任何字符?0或1次[]组{n}n次[^]否定组{n,}至少n次()分组{n,m}n~m次|或\|转义
server {
server_name www.mysite.com
listen
80;
root /home/mysite.com/html;
location
/php/ {
internal;
alias
/home/mysite.com/php/;
}
location
/documents/ {
rewrite ^
/documents/(.*)$
/php/$
1;
}
}
条件结构
server {
if (
$string) {}
if (
$request_method = POST) {
}
if (
$request_filename ~
"\.php$") {
}
if (
-f $request_filename) {
}
if (
-f $request_filename) {
break;
}
if (
-f $request_filename) {
return 404;
}
set $var1 "index .php"
if (
$var1 ~ ^(.*) (.*)$) {
set var2
$1$2;
rewrite ^ http://www.mysite.com/
$var2;
}
}
Autoindex 模块
autoindex
on;
autoindex_exact_size
on;
autoindex_localtime
on;
auth_basic模块 认证功能
auto_basic
"进入该站点需要帐号密码";
auto_basic_user_file access/password_file;
Access模块 语法跟apache一样
location {
allow
127.0.0.1;
deny
all;
}