nginx 基本配置
coremodule 全局配置 核心模块
eventmodule 事件驱动模块
httpcoremodule http内核模块–网站服务器模块
主配置文件:/etc/nginx/nginx.conf
自配置文件:/etc/nginx/conf.d/defaults.conf
1、查看nginx的版本
[root@www ~]# nginx -v
nginx version: nginx/1.14.2
nginx -V 显示版本信息和可使用模块等```
2、 检查nginx的语法是否正确
nginx -t
3、nginx 命令
nginx -s stop
nginx -s reload
4 源码nginx安装 : 下载nginx源码包>>> 解压>>> ./configure >>>>make && make install >>> 在/etc/profile 里设置nginx命令路径 >>> /etc/rc.d/rc.local 设置开机自启动
准备事项:
[root@nginx nginx-1.11.10]# yum install -y gcc gcc-c++ openssl-devel pcre-devel make zlib-devel
[root@nginx ~]# useradd nginx
[root@nginx ~]# mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
[root@nginx nginx-1.11.4]# ./configure
–prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-http_ssl_module \ #支持https加密传输
–with-http_flv_module \ #支持流媒体功能,视频等
–with-http_stub_status_module \ #支持子状态模块,显示nginx的状态
–with-http_sub_module #nginx替换网站响应内容
–with-http_gzip_static_module \ #支持压缩功能,将数据压缩之后给客户端响应,客户端收到数据,浏览器调用相应的程序自动解压,主要为了节省中间的带宽。
–with-http_auth_request_module \ #认证模块
–with-http_random_index_module #随机显示首页模块
–with-http_realip_module \ #Nginx获取真实IP模块
–http-client-body-temp-path=/var/tmp/nginx/client \ #存放客户端临时数据
–http-proxy-temp-path=/var/tmp/nginx/proxy \ #存放代理临时数据
–http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \ ##让nginx以fastcgi的机制和后端的应用程序通讯
–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ #和python相关的文件
–http-scgi-temp-path=/var/tmp/nginx/scgi \ #安全的后端通讯方式
–with-pcre \ #支持pcre正则表达式
–with-file-aio \ #支持文件的异步IO,就是异步非阻塞
–with-http_secure_link_module #支持安全链接
[root@nginx ~]# /usr/local/nginx/sbin/nginx
[root@nginx ~]# vim /etc/rc.d/rc.local
/usr/local/nginx/sbin/nginx
[root@nginx ~]# chmod a+x /etc/rc.d/rc.local
rc-local是由systemd软件提供的,但是无法使用tab键补齐
[root@nginx-source nginx-1.14.1]# systemctl start rc-local
[root@nginx-source nginx-1.14.1]# systemctl enable rc-local
源码nginx启动(直接是nginx命令路径)
/usr/local/nginx/sbin/nginx
5、nginx 配置结构
http{
server {
location{
}
}
}
二进制的nginx主配置文件在/etc/nginx/nginx.conf
子配置文件在/etc/nginx/conf.d/default.conf
7、网站根目录
location /aa {
root /usr/share/nginx/html;
index index.html;
}
client在访问时,http://192.168.122.153/ ,这个左斜杠代表的就是配置文件中root后面的目录,这个目录后面在加上客户端写的URI的地址就是寻找网站首页的地方。URI的地址就是网站主目录下面的低一级目录。也就是说client访问的实际地址是http://192.168.122.153/usr/share/nginx/html/aa 的内容。
nginx做代理服务器时,例如:
location / {
proxy_pass http://192.168.122.154;
}
以上就会代理跳转到192.168.122.154的ip上了。
改变1:location /host {
proxy_pass http://192.168.122.154/;
}
nginx代理的是192.168.122.154的网站根目录
改变2:
location ~ /host {
proxy_pass http://192.168.122.154;
}
还需要在192.168.122.154网站根目录下建立host文件夹,
以上就会代理跳转到192.168.122.154网站根目录的host文件夹下了。
8、
server {
listen 80;
server_name www.a.org a.org; #被搜索的名字
error_log /usr/local/nginx/logs/a.org/error.log error; #指定error以上的级别才需记录日志(如果全局环境已经声明,这里无需再声明)
access_log /usr/local/nginx/logs/a.org/access.log main; #main是访问日志的格式
location / {
root /a.org;# 网页存放位置
index index.html index.htm;#静态网页
}
}
nginx 隐藏版本号
在http配置中添加 : server_tokens off;
nginx 的缓存分为内存中的元数据和磁盘中的真实数据,
例如:proxy_cache_path /nginx/cache/自定义name levels=2:1:1 keys_zone=name:33m max_size=1G
整个配置的作用就是在nginx服务器本地开辟了一段用来存储缓存数据的内存空间+磁盘空间
keys_zone=name:size:定义存储键的区域(定义共享内存的名称)
keys_zone:在内存里面开辟一片区域,first是为开启的区域起的名字
20m:开启的内存大小为20m,用来保存元数据
/nginx/cache/name:一个目录名,重启nginx服务会在磁盘上自动产生,里面目录用来存放真正的缓存数据
max_size:指定/nginx/cache/name中的1G空间用于存储缓存数据 ,可根据自己的需求调整
level 2:1:2:nginx生成缓存数据会在指定的目录底下生成3级子目录,真正的缓存数据在最后一级目录里面存放,第一级第二级和第三级的子目录的名称分别是2个字符,1个字符和2个字符,且每一级子目录的名称最多是2个字符,目录名称是随机生成的。这样配置配置的好处是:缓存数据比较多的时候,分类存放,nginx的查找速度会快一些。
缓存时间:
proxy_cache_valid 200 10m; #返回值为200 缓存10分钟
proxy_cache_valid 404 1m
定义好缓存后;使用缓存功能要打开location 添加
proxy_cache name;
proxy_cache_balid 200 10m ;