2. nginx 配置语法

186 阅读2分钟

/usr/local/etc/nginx 目录下重要文件有:

  • /usr/local/etc/nginx/nginx.conf # 主配置文件

nginx 配置语法

include 语句允许把多个配置文件组合起来提升可维护性

每条指令以 ; 结尾, 指令与参数之间以空格分隔

一个 http 可以 配置多个 server 块,每个 server 块对应一个网站

# 使用 # 可以添加注释,使用 $ 符号可以使用变量
# 配置文件由指令与指令块组成,指令块以 {} 将多条组织在一起

user  nobody; # 设置运行此 nginx 用户名
worker_processes  1; # 工作进程数

#error_log  logs/error.log; # 指定错误日志的路径,日志格式
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid; # 将 pid 放到 nginx.pid 中


events {
worker_connections  1024; # 指定工作进程的最大连接数
}


http {
# include 语句允许把多个配置文件组合起来提升可维护性

include       mime.types; 
# 指定默认类型( content-type)
default_type  application/octet-stream;

# 定义日志格式的主要格式 主要为 main 后边的格式, $ 表示使用某个变量
#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

# log_format main2 "$arg_name $http_host $sent_http_date"
# access_log logs/access.log main2

# 指定访问日志的 地址 为 logs/access.log,其格式为 main 格式
#access_log  logs/access.log  main;

# 零拷贝模式,不走用户空间【是否使用零拷贝模式】
sendfile        on; 
# 是否开启 tcp 主动推送模式
#tcp_nopush     on;

# 活动链接的超时时间,单位是 s
#keepalive_timeout  0; 
keepalive_timeout  65;

# 是否启动压缩
#gzip  on;

server {
listen       8080;
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
root   html;
index  index.html index.htm;
}

# 错误页面,如果返回的状态码 404 会重定向到 404.html
#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
# 重定向服务端错误页面到静态页面 50.html 文件
error_page   500 502 503 504  /50x.html;
# = 优先级是比较高的, 当路径是 50x.html 的话,根目录是 html, 会返回 html/50x.html 文件
location = /50x.html {
root   html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
# 有些指令可以支持正则表达式
# 如果访问的文件名是 .php 结尾的话, 会把此请求转发给 http:127.0.0.1
#location ~ .php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
# 如果访问的文件名是 .php 结尾的话,会把请求转发给 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 代表一个网站
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
# 一个 server 代表一个网站
#server {
     # # 监听的端口号
#    listen       443 ssl; 
     # 指定 对应 ip 地址 的域名 
#    server_name  localhost 127.0.0.1;

#    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; # 索引文件
#    }
#}
# 包含其他配置文件,为了让该配置文件可维护性增强
include servers/*;
}