最实用的Nginx配置详解

好记忆不如烂笔头,能记下点东西,就记下点,有时间拿出来看看,也会发觉不一样的感受。

Nginx是一个非常实用的高性能的HTTP和反向代理服务器,今天小编针对Nginx的配置文件(nginx.conf)进行详细的介绍,方便各位感兴趣的朋友更便捷,更愉快的使用Nginx,配置如下:(文章末尾有示例配置,仅供参考)

一: 核心模块的常用组件

user

语法: user user [group]

缺省值: nobody nobody

指定Nginx Worker进程运行用户,默认是nobody帐号。

error_log

语法: error_log file [ debug | info | notice | warn | error | crit ]

缺省值: ${prefix}/logs/error.log

指定错误日志的存放位置和级别。

include

语法: include file | *

缺省值: none

include 指令还支持像下面配置一样的全局包含的方法,例如包含一个目录下所有以".conf"结尾的文件: include vhosts/*.conf;

pid

语法: pid file

进程id存储文件。可以使用 kill -HUP cat /var/log/nginx.pid/ 对Nginx进行配置文件重新加载。

worker_processes

语法: worker_processes number

缺省值: 1

指定工作进程数。nginx可以使用多个worker进程(建议与本机CPU核心数一致)。


二: 事件模块的常用组件

worker_connections

语法:worker_connections number

通过worker_connections和worker_proceses可以计算出maxclients: max_clients = worker_processes * worker_connections

作为反向代理,max_clients为: max_clients = worker_processes * worker_connections/4 ,因为浏览器访问时会通过连接池建立多个连接。

use

语法:use [ kqueue | rtsig | epoll | /dev/poll | select | poll | eventport ]

如果在./configure的时候指定了不止一种事件模型,

那么可以设置其中一个,以便告诉nginx使用哪种事件模型。默认情况下nginx会在./configure时找出最适合系统的事件模型。

事件模型是指Nginx处理连接的方法。


三: 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

指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录。

root和alias的区别示例

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 {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream allserver {

#ip_hash;

server 127.0.0.1:8083 down;

server 127.0.0.1:8084 weight=3;

server 127.0.0.1:8001;

server 127.0.0.1:8002 backup;

}

server {

listen 8012;

server_name localhost;

location / {

proxy_pass http://allserver;

}

}

}

ip_hash; nginx中的ip_hash技术能够将某个ip的请求定向到同一台后端,这样一来这个ip下的某个客户端和某个后端就能建立起稳固的session

1.down 表示当前的 server 暂时不参与负载

2.weight 默认为 1.weight 越大,负载的权重就越大。

3.backup: 其它所有的非 backup 机器 down 或者忙的时候,请求 backup机器。所以这台机器压力会最轻。

五: 常见变量

$arg_name

请求中的name参数

$args

请求中的参数

$binary_remote_addr

远程地址的二进制表示

$body_bytes_sent

已发送的消息体字节数

$content_length

HTTP请求信息里的"Content-Length"

$content_type

请求信息里的"Content-Type"

$document_root

针对当前请求的根路径设置值

$document_uri

与$uri相同; 比如 /test2/test.php

$host

请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名

$hostname

机器名使用 gethostname系统调用的值

$http_cookie

cookie 信息

$http_referer

引用地址

$http_user_agent

客户端代理信息

$http_via

最后一个访问服务器的Ip地址。

$http_x_forwarded_for

相当于网络访问路径

$is_args

如果请求行带有参数,返回“?”,否则返回空字符串

$limit_rate

对连接速率的限制

$nginx_version

当前运行的nginx版本号

$pidworker

进程的PID

$query_string

与$args相同

$realpath_root

按root指令或alias指令算出的当前请求的绝对路径。其中的符号链接都会解析成真是文件路径

$remote_addr

客户端IP地址

$remote_port

客户端端口号

$remote_user

客户端用户名,认证用

$request

用户请求

$request_body

这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义

$request_body_file

客户端请求主体信息的临时文件名

$request_completion

如果请求成功,设为"OK";如果请求未完成或者不是一系列请求中最后一部分则设为空

$request_filename

当前请求的文件路径名,比如/opt/nginx/www/test.php

$request_method

请求的方法,比如"GET"、"POST"等

$request_uri

请求的URI,带参数;比如http://localhost:88/test1/

$scheme

所用的协议,比如http或者是https

$server_addr

服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费)

$server_name

请求到达的服务器名

$server_port

请求到达的服务器端口号

$server_protocol

请求的协议版本,“HTTP/1.0"或"HTTP/1.1”

$uri

请求的URI,可能和最初的值有不同,比如经过重定向之类的

小编的示例配置:

user mask; # 使用的用户和组

worker_processes 4; #工作进程数(建议与本机CPU核心数一致)

#error_log logs/error.log;

#error_log logs/error.log notice;

error_log logs/error.log info;

pid logs/nginx.pid;#nginx 进程id存储文件

events {

worker_connections 1024; #每个worker的最大连接数

}

http {

include mime.types; #包含一个文件描述了:不同文件后缀对应的MIME,见案例分析

default_type application/octet-stream; #制定默认MIME类型为二进制字节流

#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; #指令 access_log 指派路径、格式和缓存大小。

sendfile on; #开启调用Linux的sendfile(),提供文件传输效率

#tcp_nopush on; #是否允许使用socket的TCP_NOPUSH或TCP_CORK选项

#keepalive_timeout 0;

keepalive_timeout 65; #指定客户端连接为长连接时保持活动的超时时间,在这个时间之后,服务器会关掉连接。

# gzip on; #设置gzip,压缩文件

# lua_code_cache off;

include conf.d/*.conf; #引入其他的配置文件

#配置一台虚拟机

server {

listen 80;

server_name 127.0.0.1;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root html;

index index.htm mis.html index.html ;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}


猜你喜欢

转载自blog.csdn.net/supingemail/article/details/79581153