Nginx基础入门篇-基础配置
目录
Nginx配置文件
Nginx编译参数
Nginx基本配置
创建一个网站
Nginx配置文件
通过 rpm -ql nginx查询
/etc/logrotate.d/nginx #日志轮转
/etc/nginx/nginx.conf #主配置文件
/etc/nginx/conf.d #子配置文件
/etc/nginx/conf.d/default.conf #默认的网站配置文件
/etc/nginx/fastcgi_params #动态网站模块文件-python,php所需的相关变量
/etc/nginx/scgi_params #传递给 SCGI 服务器的标准参数
/etc/nginx/uwsgi_params #传递给 uWSGI 服务器的标准参数
/etc/nginx/koi-utf #字符集,文件编码
/etc/nginx/win-utf #Nginx 为 Windows 系统提供的字符编码配置文件
/etc/nginx/koi-win #是一个字符集映射文件,可以在KOI8-R和Windows-1251编码之间进行转换
/etc/nginx/mime.types #网站文件类型和相关处理程序
/usr/lib64/nginx/modules #第三方模块
/usr/lib/systemd/system/nginx.service #脚本服务
/usr/sbin/nginx #主程序
/usr/sbin/nginx-debug #nginx的调试程序
/usr/share/doc/nginx-1.12.1 #文档
/usr/share/doc/nginx-1.12.1/COPYRIGHT #包含基于标准Nginx发行版
/usr/share/man/man8/nginx.8.gz #man手册
/usr/share/nginx #nginx在许多Linux发行版上用于存储静态文件、文档和其他共享资源的标准位置
/usr/share/nginx/html #许多Linux发行版上nginx的默认web根目录包含Nginx默认提供给客户端的静态文件
/usr/share/nginx/html/50x.html #nginx为5xx服务器错误提供的默认错误页面
/usr/share/nginx/html/index.html #默认主页
/var/cache/nginx #nginx在使用代理缓存或其他缓存功能时存储缓存内容的地方
/var/log/nginx #nginx的日志文件夹
/usr/lib64/nginx #nginx模块目录
Nginx编译参数
通过nginx -V查看
configure arguments: #配置参数./configure --help查询帮助
--prefix=/etc/nginx #安装路径
--sbin-path=/usr/sbin/nginx #程序文件
--modules-path=/usr/lib64/nginx/modules #模块路径
--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=/run/lock/subsys/nginx #锁路径,防止重复安装nginx
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body #缓存
--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 #指定nginx为scgi(简单通用网关接口)请求存储临时文件的目录。
--with-compat #启用动态模块兼容性
--user=nginx #用户
--group=nginx #组
--with-file-aio #是异步非阻塞,大大提高性能
--with-threads #多线程模块
--with-http_addition_module #响应之前或者之后追加文本内容
--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 #启用Nginx的gzip静态模块,该模块允许提供预压缩文件(.gz文件),而不是动态压缩文件。
--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 #可在Nginx中启用http/2支持。
--with-mail #邮件客户端
--with-mail_ssl_module #Nginx的邮件代理模块启用ssl/TLS支持
--with-stream #负载均衡模块
--with-stream_realip_module #启用Nginx的stream Real IP模块。此模块允许Nginx修改流(TCP/UDP)代理连接中的客户端IP地址
--with-stream_ssl_module #Nginx中的TCP和UDP流连接启用ssl/TLS终止
--with-stream_ssl_preread_module #启用Nginx的stream ssl预读模块。该模块允许Nginx从SSL/TLS握手中提取信息(如服务器名称指示-SNI),而无需解密流量,从而为加密流启用基于内容的路由。
Nginx基本配置
主配置文件/etc/nginx/nginx.conf内容说明
CoreModule核心模块(进程数):配置影响nginx全局的指令。般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
EventsModule事件驱动模块(工作模式):配置影响nginx服务器或与用户的网络连接,有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
HttpCoreModule http内核模块(文档程序类型,配置文件等)
http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
server块:配置虚拟主机的相关参数,一个http中可以有多个server。
location块:配置请求的路由,以及各种页面的处理情况
user nginx; #运行nginx程序的独立账号
worker_processes auto; #启动的worker进程数量(CPU数量一致或auto)error_log /var/log/nginx/error.log notice; #错误日志存放位置
pid /run/nginx.pid; #nginx的pidevents { #事件use epoll; #事件驱动模块epollworker_connections 1024; #每个worker进程允许处理的最大连接数
}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; #包含子配置文件夹
}
默认虚拟机配置文件
server { #默认网站配置文件listen 80; #监听端口server_name localhost; #FQDN#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$ { #匹配所有以.php结尾的请求
# proxy_pass http://127.0.0.1; #代理#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 #动态网站设置##location ~ \.php$ { #匹配所有以.php结尾的请求# root html; #设置PHP文件的根目录# fastcgi_pass 127.0.0.1:9000; #指定PHP-FPM服务器的地址# fastcgi_index index.php; #设置默认的索引文件名称# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #告诉PHP-FPM要执行哪个PHP文件# include fastcgi_params; #引入标准的FastCGI参数文件#}# deny access to .htaccess files, if Apache's document root #访问控制部分# concurs with nginx's one##location ~ /\.ht { #匹配所有包含.ht的请求路径# deny all; #拒绝所有访问请求#}
}Type :qa and press <Enter> to exit Vim 44,0-1 底端
创建一个网站
vim /etc/nginx/conf.d/ts.conf server { #虚拟主机
listen 80; #监听端口
server_name ts.com; #服务器名称
location / { #网站目录设置
root /ts; #网站主目录在本地的路径
index index.html ; #主页文件名
}
}
mkdir /ts #创建存放网站目录的文件夹
echo 11111 > /ts/index.htmlsystemctl restart nginx#做域名解析和访问
http{} 是整个服务器,所有网站的设置。
server{}是某一个网站的设置。
location{} 是某一个页面的设置。
