nginx基础

nginx基础

安装
  # yum -y install openssl openssl-devel pcre-devel libxml2-devel libxslt-devel
# groupadd www
# useradd -g www www
# tar xf nginx-1.4.4.tar.gz
# cd nginx-1.4.4

# ./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--without-select_module \
--without-poll_module \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_xslt_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-cc=`which gcc`

# make
# make install
# ln -s /usr/local/nginx1.10 /usr/local/nginx
# /usr/local/nginx/sbin/nginx
# netstat -tnlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11051/nginx
# elinks -dump http://localhost
Welcome to nginx!
# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local 

配置文件

  rpm -ql nginx
/etc/logrotate.d/nginx 日志轮转
/etc/nginx 配置文件总目录
/etc/nginx/conf.d 子配置文件夹
/etc/nginx/conf.d/default.conf 默认的网站配置文件
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types 关联程序 网站文件类型 和 相关处理程序
/etc/nginx/modules 模块文件夹。第三方模块
/etc/nginx/nginx.conf 主配置
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service systemctl 服务脚本。
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx 主程序
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.12.1 文档
/usr/share/doc/nginx-1.12.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz man 手册
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html 默认主页
/var/cache/nginx 缓存各种
/var/log/nginx 日志文件夹
缓存
ls /var/cache/nginx/
client_temp fastcgi_temp proxy_temp scgi_temp uwsgi_temp
日志
ls /var/log/nginx/
access.log error.log
配置文件
rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug

 

编译参数

 nginx -V


nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: 配置参数./configure --help查询帮助
--prefix=/etc/nginx 安装路径
--sbin-path=/usr/sbin/nginx 程序文件
--modules-path=/usr/lib64/nginx/module 模块路径
--conf-path=/etc/nginx/nginx.conf 配置文件
--error-log-path=/var/log/nginx/error.log 错误日志
--http-log-path=/var/log/nginx/access.log 访问日志
--pid-path=/var/run/nginx.pid 程序ID
--lock-path=/var/run/nginx.lock
锁路径,防止重复启动nginx
--http-client-body-temp-path=/var/cache/nginx/client_temp
缓存
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
代理缓存
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
php缓存
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
python缓存
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx 用户
--group=nginx 组

--with-compat
动态模块的兼容性
--with-file-aio
Nginx启用aio,需要在配置编译时,加上相应选项否则启动Nginx会报错unknown directive “aio”
--with-threads
多线程模块
--with-http_addition_module
响应之前或者之后追加文本内容,比如想在站点底部追加一个js或者css
--with-http_auth_request_module
认证模块
--with-http_dav_module
增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭
--with-http_flv_module
NGINX 添加MP4、FLV视频支持模块
--with-http_gunzip_module
压缩模块
--with-http_gzip_static_module
--with-http_mp4_module
多媒体模块
--with-http_random_index_module
nginx显示随机首页模块
--with-http_realip_module
Nginx获取真实IP模块
--with-http_secure_link_module
nginx安全下载模块
--with-http_slice_module
nginx中文文档
--with-http_ssl_module
安全模块
--with-http_stub_status_module
访问状态
--with-http_sub_module
nginx替换网站响应内容
--with-http_v2_module
--with-mail
邮件客户端
--with-mail_ssl_module
--with-stream
nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发、代理或者负载均衡等。
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_module

--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --
param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
模块类
基本配置
CPU优化参数

 

 

Nginx 基本配置

 

1 分类


CoreModule 核心模块 (进程数等)
EventsModule 事件驱动模块(工作模式等)
HttpCoreModule http内核模块(文档程序类型,配置文件等

2 观察主配置文件


vim /etc/nginx/nginx.conf
user nginx;
worker_processes 1; 启动的worker进程数量(CPU数量一致或auto)
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
use epoll; 事件驱动模型epoll【默认】
worker_connections 1024; //每个worker进程允许处理的最大连接数,例如10240,65535
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on; 优化参数 高效传输文件的模式
#tcp_nopush on; 优化参数
keepalive_timeout 65; 优化参数
#gzip on; 压缩参数 是否开启压缩
include /etc/nginx/conf.d/*.conf; 包含子配置文件夹

3 观察默认虚拟主机配置文件


vim /etc/nginx/conf.d/default.conf
server { 默认网站配置文件
listen 80; 监听端口
server_name localhost; FQDN
#charset koi8-r; 网页字符类型
#access_log /var/log/nginx/host.access.log main; 日志
location / {
root /usr/share/nginx/html; 主目录
index index.html index.htm; 默认主页名
}
#error_page 404 /404.html; 错误页面
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html; 错误页面
location = /50x.html { 错误页面
root /usr/share/nginx/html; 错误页面主目录
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80 代理设置
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 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
#location ~ /\.ht {
# deny all;
#}
}

启动一个新的虚拟主机

1 vim /etc/nginx/conf.d/xuleilinux.conf
server{
listen 80;
server_name xuleilinux.com;
location /{
root /xuleilinux;
index index.html index.htm 2.html;
}
}
server 虚拟主机
listen 监听端口
server_name 服务器名称
location 网站目录设置
root 网站主目录在本地的路径
index 主页文件名
http{} 是整个服务器,所有虚拟主机的设置。
server{}是某一个虚拟主机的设置
location{} 是某一个页面的设置。
2 重启服务
3 域名解析和访问

 

 

 

Nginx 模块Modules

 

 

 

stub_status_module


目的
展示nginx链接状态
查询
nginx -V 2>&1 | grep stub_status
--with-http_stub_status_module
启动状态模块

1 访问默认站点的状态模块


http://10.18.41.64/nginx_status
未启用

2

 

配置状态模块


vim /etc/nginx/nginx.conf
server {
请写在server内
location /nginx_status {
stub_status;
allow all;
}
启用该模块
}

3 重启服务再次访问


systemctl restart nginx
观察连接数 和请求数。
解释
Active connections: 22 当前活动的连接数
server accepts handled requests
17 17 24
17 总连接数connection(TCP)

17 成功的连接数connection (TCP)
失败连接=(总连接数-成功连接数)
24 总共处理的请求数requests(HTTP)

connection 连接数,tcp连接
request http请求,GET/POST/DELETE/UPLOAD
Reading: 2 读取客户端Header的信息数 请求头
Writing: 1 返回给客户端的header的信息数 响应头
Waiting: 19 等待的请求数,开启了keepalive
关于链接的问题
1.什么是链接
2.什么是请求
3.keepalived
长连接设置
Httpd守护进程,一般都提供了keep-alive timeout时间设置参数。比如nginx的keepalive_timeout,和Apache的KeepAliveTimeout。这个 keepalive_timout时间值意味着:一个http产生的tcp连接在传送完最后一个响应后,还需要hold住 keepalive_timeout秒后,才开始关闭这个连接。当httpd守护进程发送完一个响应后,理应马上主动关闭相应的tcp连接,设置 keepalive_timeout后,httpd守护进程会想说:”再等等吧,看看浏览器还有没有请求过来”,这一等,便是 keepalive_timeout时间。如果守护进程在这个等待的时间里,一直没有收到浏览发过来http请求,则关闭这个http连接。

 

 

4.关闭长连接会如何


systemctl restart nginx

 

 

5.再次刷新状态也观察






 

 

random_index_module


目的
将主页设置成随机页面,是一种微调更新机制
启动随机主页

1 创建主页目录


mkdir /app
cd /app

2 创建多个主页


touch blue.html green.html red.html .yellow.html
在不同的页面书写不同的内容,例如
<html>
<head>
<title>green color</title>
</head>
<body style="
<h1>green color!</h1>
</body>
</html>

3 启动随机主页


# vim /etc/nginx/conf.d/default.conf
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
root /app;
random_index on;
}
完成该试验后,请注释掉该功能。
避免影响其他实验。
systemctl restart nginx
刷新主页,观察变化

4 请注意隐藏文件并不会被随机选取




sub_module


目的
网页内容替换
如果我们用模板生成网站的时候,因为疏漏或者别的原因造成代码不如意,但是此时因为文件数量巨大,不方便全部重新生成,那么这个时候我们就可以用此模块来暂时实现纠错。另一方面,我们也可以利用这个实现服务器端文字过滤的效果。
启动替换1
vim /etc/nginx/nginx.conf
启动nginx默认页面
server {
在server{下面插入
sub_filter nginx 'QianFeng';
sub_filter_once on;
说明
替换模块 将nginx 替换成 QianFeng
单次替换 开启
重启服务,测试页面

1.只替换了一处。

2.将单次替换关闭,再次刷新页面,即可看见全文替换。


sub_filter_once off

启动替换2


1 准备一个被替换的java脚本
vim /usr/share/nginx/html/hello.js
function Hello() {
alert("Hello World")
}
2 主页插入按钮
<body>
<h1>Welcome to <strong>nginx</strong> on Fedora!</h1>
<input type="button" οnclick="Hello()" value="say hellp" />
请注意body和h1是已有的。
3 启动替换
sub_filter nginx 'QianFeng';
sub_filter </head> '</head><script type="text/javascript" src="hello.js"></script>';
sub_filter_once off;
在刚才的实验旁边,再加一条sub_filter
4 重启服务,测试页面。

http详解:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Overview

 

转载于:https://www.cnblogs.com/chunjianghuayueye369/p/9576841.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值