nginx基本运用介绍(一)
nginx介绍可以看百度百科,其官方网站如下:
http://nginx.org/
我们这里与前面的docker结合起来学习,因为后期我们项目的开发肯定是基与docker的
nginx基于docker安装和运行
1.下载最新的nginx镜像
docker pull nginx
2.运行nginx容器
docker run -d -p 80:80 9beeba249f3e
浏览器中输入我们的服务器ip地址即可以看到nginx首页
3.重启和关闭nginx
docker restart a3880bde71b4
docker stop a3880bde71b4
4.进入容器
docker exec -it a3880bde71b4 /bin/bash
nginx原理和配置文件
nginx安装目录
我们如何来找nginx的安装目录呢,有两种方法;
第一种:通过dockerfile的定义来看;
第二种,直接搜索 find / -name nginx
我们可以一个一个进去看 /etc/nginx 下 就是nginx安装目录;
这个nginx.conf是nginx默认的主配置文件;
还有/var/log/nginx 目录是 nginx默认的日志目录;
挂载容器目录启动nginx容器
为了方便我们修改配置文件
我们启动容器的时候,需要挂载容器目录,这样可以在宿主机中修改配置,来实现同步容器里的文件
首先第一步:我们需要copy下原始数据
docker cp a3880bde71b4:/etc/nginx /home/data/
然后我们关闭容器
docker stop a3880bde71b4
最后启动容器,挂载目录
docker run -it --name=myNginx -v /home/data/nginx:/etc/nginx -p 80:80 9beeba249f3e
nginx配置文件nginx.conf介绍
基本配置:
user nginx; #配置worker进程运行用户
worker_processes 1; #配置工程进程数目,根据硬件配置,一般是和CPU数量一致,或者CPU数量的2倍,能达到最佳性能
error_log /var/log/nginx/error.log warn; # 配置全局错误日志文件以及配置级别 [ debug | info | notice | warn | error | crit ]
pid /var/run/nginx.pid; #配置进程pid文件
关于日志级别:
在配置nginx.conf 的时候,有一项是指定错误日志的,默认情况下你不指定也没有关系,因为nginx很少有错误日志记录的。但有时出现问题时,是有必要记录一下错误日志的,方便我们排查问题。
error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit, 该级别在日志名后边定义格式如下:
error_log /your/path/error.log crit;
crit 记录的日志最少,而debug记录的日志最多。如果你的nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log并没有看到有意义的信息,那么就可以调一下错误日志的级别,当你调成error级别时,错误日志记录的内容会更加丰富。
events 是配置工作模式和连接数
events {
worker_connections 1024; # 配置每个worker进程连接数上限
}
说明:nginx支持得总连接数=worker_processes * worker_connections
配置http服务器
http {
include /etc/nginx/mime.types; # 配置nginx支持哪些多媒体类型
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; #开启gzip压缩输出
include /etc/nginx/conf.d/*.conf;
}
http里面的server配置(可以多个)其在上面介绍的include里面
配置server服务器;可以多个;
server {
listen 80; #监听端口
server_name localhost; # 配置服务名
#charset koi8-r; #配置字符集
#access_log /var/log/nginx/host.access.log main; #配置本虚拟主机访问日志
# 匹配/请求 ,/是根路径请求,会被该location匹配到并且处理
location / {
root /usr/share/nginx/html; #root是配置服务器的默认网关根目录位置
index index.html index.htm; #配置首页文件的名称
}
#error_page 404 /404.html; #配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #配置50x错误页面
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;
#}
}
到此我们的nginx第一章介绍到这,主要讲了其配置文件里面的个个命令概念,欢迎留言评论,感兴趣麻烦关注下博主公众号 非常谢谢!