本文为看视频《Nginx入门到实践》总结的学习笔记。
一、服务器基础配置
远程链接服务器
ssh 用户名@公网ip
默认的用户名是root,假如公网 ip 是 a.b.c.d, 那链接命名就是
ssh root@a.b.c.d
下载安装基础库
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
关闭 iptables
查看iptables规则
iptables -L
或
iptables -t nat -L
关闭 iptables 规则
iptables -F
或
iptables -t nat -F
关闭 SELinux
查看是否打开
getenforce
关闭
setenforce 0
二、Nginx 简介及安装
Nginx 是一个开源且高性能、高可靠的 HTTP 中间件、代理服务。
安装Nginx
打开官网 https://nginx.org/en/linux_packages.html#stable
To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo
with the following contents:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/ gpgcheck=0 enabled=1
Replace “OS
” with “rhel
” or “centos
”, depending on the distribution used, and “OSRELEASE
” with “6
” or “7
”, for 6.x or 7.x versions, respectively.
三、安装目录及配置讲解
3.1 安装目录讲解
查看nginx的所有安装目录
rpm -ql nginx
然后得到如下配置
[root@ ~]# rpm -ql nginx
nginx日志轮转,用于logrotate服务的日志切割
/etc/logrotate.d/nginx
nginx主配置文件
/etc/nginx/nginx.conf
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
cgi配置相关,fastcgi配置
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
编码转换映射转化文件
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf
设置http协议的 Content-Type 与扩展名对应关系
/etc/nginx/mime.types
用于配置出系统守护进程管理器管理方式
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
nginx模块目录
/etc/nginx/modules
/usr/lib64/nginx/modules
/usr/lib64/nginx
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
nginx服务的启动管理的终端命令
/usr/sbin/nginx
/usr/sbin/nginx-debug
nginx的手册和帮助文件
/usr/share/doc/nginx-1.14.0
/usr/share/doc/nginx-1.14.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
nginx 的缓存目录
/var/cache/nginx
nginx日志目录
/var/log/nginx
3.2 安装编译参数
命令 nginx -V
查看所有编译参数
3.3 Nginx 默认配置语法
参数 | 说明 |
---|---|
user | 设置nginx服务的系统使用用户 |
worker_processes | 工作进程数(一般与服务器核数保持一致) |
rror_log | nginx的错误日志 |
pid | nginx服务启动时候pid |
events -> worker_connections | 每个进程允许最大连接数 |
events -> use | 工作进程数 |
nginx 的默认配置文件
文件路径 /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#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 listenin