设置worker进程的用户,指的linux中的用户,会涉及到nginx操作目录或文件的一些权限,默认为 nobody

user root;

worker进程工作数设置,一般来说CPU有几个,就设置几个,或者设置为

N-1也行 

worker_processes 1;

nginx 日志级别 debug | info | notice | warn | error | crit | alert | emerg ,错误级别从左到右越来越大

设置nginx进程 pid 

pid logs/nginx.pid;

设置工作模式 

events {     # 默认使用epoll     use epoll;     # 每个worker允许连接的客户端最大连接数     worker_connections 10240; }

http 是指令块,针对http网络传输的一些指令配置 

http { }

include 引入外部配置,提高可读性,避免单个配置文件过大 

include mime.types;

log_format 设定日志格式, main 为定义的格式名称,如此 access_log 就可以直接使用这个变量了,前提是要去掉log_format跟access_log的注释

telnet nginx端口没有返回 nginx端口设置_nginx

参数名

参数意义

$remote_addr

客户端ip

$remote_user

远程客户端用户名,一般为:’-’

$time_local

时间和时区

$request

请求的url以及method

$status

响应状态码

$body_bytes_send

响应客户端内容字节数

$http_referer

记录用户从哪个链接跳转过来的

$http_user_agent

用户所使用的代理,一般来时都是浏览器

$http_x_forwarded_for

通过代理服务器来记录客户端的ip

然后就可以在 logs/access_log 中查看日志信息。

sendfile 使用高效文件传输,提升传输性能。启用后才能使用 tcp_nopush ,是指当数据表累积一定大小后才发送,提高了效率。

sendfile on; tcp_nopush on;

keepalive_timeout 设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗。

#keepalive_timeout 0; keepalive_timeout 65;

server是虚拟主机

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   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }

注意:当然server块可以不止一个,我们通过新增server块来增加虚拟主机数

实现80端口则路由到index.html,89端口则路由到jihoo.html

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   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }server {        listen       89;        server_name  localhost;        location / {                root   html;                index  jihoo.html;        }  }

jihoo.html 的内容如下:

telnet nginx端口没有返回 nginx端口设置_nginx_02

重启nginx:

./sbin/nginx -s reload

80端口:

telnet nginx端口没有返回 nginx端口设置_telnet nginx端口没有返回_03

89端口:

telnet nginx端口没有返回 nginx端口设置_html_04

随着虚拟主机数的增加,会导致配置文件越来越大,不便阅读,可以通过include 的方式来处理。

在配置文件目录下新建外部文件 jihoo.conf,插入89端口的server块

[root@master conf]# cat ../conf/jihoo.conf server {        listen       89;        server_name  localhost;        location / {                root   html;                index  jihoo.html;        }  }[root@master conf]#

将配置文件修改为

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   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }         include jihoo.conf;

重启nginx:

./sbin/nginx -s reload