一,Nginx服务的主配置文件 nginx.conf

vim /usr/local/nginx/conf/nginx.conf 
1.全局配置
#user nobody; 					#运行用户,若编译时未指定则默认为 nobody
worker_processes 1; 			#工作进程数量,可配置成服务器内核数 * 2,如果网站访问量不大,一般设为1就够用了
#error_log logs/error.log; #错误日志文件的位置 #pid logs/nginx.pid; #PID 文件的位置 2.I/O 事件配置 events { use epoll; #使用 epoll 模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能 worker_connections 4096; #每个进程处理 4096 个连接 } 临时设置: #如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。 #在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。  #可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。 永久设置: /etc/security/limits.conf 末尾添加: soft nofile 59000 hard nofile 60000  #epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。 若工作进程数为 8,每个进程处理 4 096 个连接,则允许 Nginx 正常提供服务的连接数 已超过 3 万个(4 096×8=32 768),当然具体还要看服务器硬件、网络带宽等物理条件的性 能表现。 3.HTTP 配置 使用“http { }”界定标记,包括访问日志、HTTP 端口、网页目录、默认字符集、连接保 持,以及后面要讲到的虚拟 Web 主机、PHP 解析等一系列设置,其中大部分配置语句都包 含在子界定标记“server { }”内 http {  ##文件扩展名与文件类型映射表 include mime.types;  ##默认文件类型 default_type application/octet-stream;  ##日志格式设定 #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;  ##支持文件发送(下载) sendfile on;  ##此选项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用 #tcp_nopush on;  ##连接保持超时时间,单位是秒 #keepalive_timeout 0; keepalive_timeout 65;  ##gzip模块设置,设置是否开启gzip压缩输出 #gzip on;  ##Web 服务的监听配置 server {  ##监听地址及端口 listen 80;  ##站点域名,可以有多个,用空格隔开 server_name www.ceshi.com;  ##网页的默认字符集 charset utf-8;  ##根目录配置 location / {  ##网站根目录的位置/usr/local/nginx/html root html;  ##默认首页文件名 index index.html index.php; }  ##内部错误的反馈页面 error_page 500 502 503 504 /50x.html;  ##错误页面配置 location = /50x.html { root html; } } } ---------------------------------------------------------------------------------------------------------- 日志格式设定: $remote_addr与$http_x_forwarded_for用以记录客户端的ip地址; $remote_user:用来记录客户端用户名称; $time_local: 用来记录访问时间与时区; $request: 用来记录请求的url与http协议; $status: 用来记录请求状态;成功是200, $body_bytes_sent :记录发送给客户端文件主体内容大小; $http_referer:用来记录从哪个页面链接访问过来的; $http_user_agent:记录客户浏览器的相关信息; 通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了, 通过$remote_add拿到的IP地址是反向代理服务器的iP地址。 反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息, 用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。 location常见配置指令,root、alias、proxy_pass root(根路径配置):root /var/www/html 请求www.ceshi.com/test/1.html,会返回文件/var/www/html/test/1.html alias(别名配置):alias /var/www/html 请求www.ceshi.com/test/1.html,会返回文件/var/www/html/1.html proxy_pass(反向代理配置) 


二,访问状态统计配置

1.先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
cat /opt/nginx-1.12.0/auto/options | grep YES     #可查看 nginx 已安装的所有模块

2.修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.ceshi.com;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
		}
		##添加 stub_status 配置## location /status { #访问位置为/status stub_status on; #打开状态统计功能 access_log off; #关闭此位置的日志记录 } } } 3.重启服务,访问测试 systemctl restart nginx 浏览器访问 http://192.168.10.10/status Active connections :表示当前的活动连接数; server accepts handled requests :表示已经处理的连接信息, 三个数字依次表示已处理的连接数、成功的TCP握手次数、 已处理的请求数。 可 curl -s http://192.168.10.10/status | awk '/Active/{print $3}' | if 结合 awk与if 语句进行性能监控。 


三,基于授权的访问控制

1.生成用户密码认证文件
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db chmod 400 /usr/local/nginx/passwd.db 2.修改主配置文件相对应目录,添加认证配置项 vim /usr/local/nginx/conf/nginx.conf ...... server { location / { ...... ##添加认证配置## auth_basic "secret"; #设置密码提示框文字信息 auth_basic_user_file /usr/local/nginx/passwd.db; } } 3.重启服务,访问测试 nginx -t systemctl restart nginx 浏览器访问 http://192.168.10.10 

四,基于客户端的访问控制

访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问。 allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问。 规则从上往下执行,如匹配则停止,不再往下匹配。 vim /usr/local/nginx/conf/nginx.conf ...... server { location / { ...... ##添加控制规则## allow 192.168.10.200; #允许访问的客户端 IP deny all; #拒绝其它IP客户端访问 } } systemctl restart nginx 

五,三种Nginx虚拟主机

1.基于域名的 Nginx 虚拟主机

1.为虚拟主机提供域名解析
echo "192.168.10.10 www.ceshi.com www.mhy.com" >> /etc/hosts

2.为虚拟主机准备网页文档
mkdir -p /var/www/html/mhy
mkdir -p /var/www/html/ceshi
echo "<h1>www.ceshi.com</h1>" > /var/www/html/ceshi/index.html echo "<h1>www.mhy.com</h1>" > /var/www/html/mhy/index.html 3.修改Nginx的配置文件 vim /usr/local/nginx/conf/nginx.conf ...... http { ...... server { listen 10; server_name www.ceshi.com; #设置域名www.ceshi.com charset utf-8; access_log logs/www.ceshi.access.log; #设置日志名 location / { root /var/www/html/ceshi; #设置www.ceshi.com 的工作目录 index index.html index.php; } error_page 500 502 503 504 /50x.html; location = 50x.html{ root html; } } server { listen 10; server_name www.mhy.com; #设置域名www.mhy.com charset utf-8; access_log logs/www.mhy.access.log; location / { root /var/www/html/mhy; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = 50x.html{ root html; } } } 4.重启服务,访问测试 systemctl restart nginx 浏览器访问 http://www.ceshi.com http://www.mhy.com 

2.基于IP 的 Nginx 虚拟主机

ifconfig ens33:0 192.168.10.11 netmask 255.255.255.0 vim /usr/local/nginx/conf/nginx.conf ...... http { ...... server { listen 192.168.10.10:10; #设置监听地址192.168.10.10 server_name www.ceshi.com; charset utf-8; access_log logs/www.ceshi.access.log; location / { root /var/www/html/ceshi; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = 50x.html{ root html; } } server { listen 192.168.10.11:10; #设置监听地址192.168.10.11 server_name www.mhy.com; charset utf-8; access_log logs/www.mhy.access.log; location / { root /var/www/html/mhy; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = 50x.html{ root html; } } } systemctl restart nginx 浏览器访问 http://192.168.10.10 http://192.168.10.11 

3.基于端口的 Nginx 虚拟主机

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.10.10:1010; #设置监听 1010 端口 server_name www.ceshi.com; charset utf-8; access_log logs/www.ceshi.access.log; location / { root /var/www/html/ceshi; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = 50x.html{ root html; } } server { listen 192.168.10.10:8888; #设置监听 8888 端口 server_name www.mhy.com; charset utf-8; access_log logs/www.mhy.access.log; location / { root /var/www/html/mhy; index index.html index.php; } error_page 500 502 503 504 /50x.html; location = 50x.html{ root html; } } } systemctl restart nginx 浏览器访问 http://192.168.10.11:1010 http://192.168.10.11:8888 


原文地址:http://www.cnblogs.com/y0226/p/16888130.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性