1、下载 Nginx.org
2、安装及启动服务
将下载的 nginx-1.24.0.tar.gz 包上传到服务器解压(/user/local...)
tar -zxvf nginx-1.24.0.tar.gz
更改配置
cd nginx-1.24.0 // 进入 nginx 目录
./configure // 执行配置文件
make install // 安装
whereis nginx // 查找 nginx 的安装位置
启动服务
# 启动 nginx
service nginx start
外部访问不了(关闭防火墙或放行对应端口)
# 1、关闭防火墙
systemctl stop firewalld.service
# 2、放行对应端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=443/tcp --permanent
# 重载
firewall-cmd --reload
# 查看状态
firewall-cmd --list-all
3、常用命令
# 启动 nginx 服务
service nginx start
# 停止 nginx 服务
service nginx stop
# 重启 nginx 服务
service nginx restart
# 查看 nginx 运行状态
service nginx status
4、常用配置
-
全局块:配置影响 nginx 全局的指令
- 日志存放路径
- 配置文件引入
- 运行 nginx 服务器的用户组
- nginx 进程 pid 存放路径
- 允许生成 worker process 数
-
events块:配置影响 nginx 服务器或与用户的网络连接
- 有每个进程的最大连接数
- 选取哪种事件驱动模型处理连接请求
- 是否允许同时接受多个网路连接
- 开启多个网络连接序列化等
-
http块:可以嵌套多个server
- 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
- 如文件引入,mime-type 定义
- 日志自定义
- 是否使用 sendfile 传输文件
- 连接超时时间,单连接请求数等
-
server块:
- 配置虚拟主机的相关参数
- 一个 http 中可以有多个 server
-
location块:配置请求的路由,以及各种页面的处理情况
##
# 全局配置
##
# user nginx lion; # 指定运行 Nginx 的 woker 子进程的 属主 和 属组 可不进行设置
worker_processes auto; # Nginx 进程数,一般设置为和 CPU 核数一样
error_log /var/log/nginx/error.log warn; # Nginx 的错误日志存放目录
pid /var/run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
worker_rlimit_nofile 65535; # 每个 worker 子进程的最大连接数量
worker_cpu_affinity 0001 0010 0100 1000; # 将每个 worker 子进程与我们的 cpu 物理核心绑定
##
# events 模块
##
events {
# use epoll; # 事件驱动模型 select、poll、kqueue、epoll、/dev/poll、eventport
worker_connections 65535; # 子进程能够处理的最大并发连接数
accept_mutex on # 默认是off关闭的,这里推荐打开
}
##
# http 模块
##
http {
##
# 基础相关设置
##
sendfile on; # 开启高效传输模式
tcp_nopush on; # 减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 60 50; # 保持连接的时间、超时时间
send_timeout 10s; #
types_hash_max_size 2048;
client_header_buffer_size 4k;
client_max_body_size 8m;
include /etc/nginx/mime.types; # 文件扩展名与类型映射表
default_type application/octet-stream; # 默认文件类型
##
# 日志相关配置
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# 解压相关配置
##
# 是否开启 gzip
gzip on;
# 要采用 gzip 压缩的 MIME 文件类型,其中 text/html 被系统强制启用;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# 开启后首先检查 gz 结尾的文件,如果有则直接返回该 .gz 文件内容;
gzip_static on;
# 用于设置启用或禁用从代理服务器上收到相应内容 gzip 压缩;
gzip_proxied any;
# 使代理服务器根据请求头中的 Accept-Encoding 识别是否启用 gzip 压缩;
gzip_vary on;
# gzip 压缩比
gzip_comp_level 6;
# 获取多少内存用于缓存压缩结果,16 8k 表示以 8k*16 为单位获得;
gzip_buffers 16 8k;
# 允许压缩的页面最小字节数,页面字节数从header头中的 Content-Length 中进行获取
gzip_min_length 1k;
# 默认 1.1,启用 gzip 所需的 HTTP 最低版本;
gzip_http_version 1.1;
##
# 访问配置
##
server {
listen 80; # 监听默认的 80 端口(可自定义)
server_name localhost; # 定义网站域名
##
# 配置 https
##
listen 443 ssl http2 default_server; # SSL 访问端口号为 443
server_name lion.club; # 填写绑定证书的域名(我这里是随便写的)
ssl_certificate /etc/nginx/https/lion.club_bundle.crt; # 证书地址
ssl_certificate_key /etc/nginx/https/lion.club.key; # 私钥地址
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 支持 ssl 协议版本
##
# 访问资源
##
# - = 精确匹配;
# - ~ 正则匹配,区分大小写;
# - ~* 正则匹配,不区分大小写;
# - ^~ 匹配到即停止搜索;
location /image {
root /opt/nginx/static;
}
location ~ .(jpeg|jpg|png|svg)$ {
root /usr/share/nginx/images;
}
# 报错访问
error_page 404 /404.html; # path 找不到会访问 404.html
error_page 500 502 503 504 /50x.html; # 50* 错误会访问 50x.html
location = /50x.html {
root html;
}
# 页面访问
location / {
# 定义网站根目录(相对或绝对路径)
# 1、相对路径:./
# 2、绝对路径:/var/www/html
root html;
// 定义站点的默认页
index index.html index.htm;
}
deny 172.168.22.11; # 禁止访问的ip地址,可以为all
allow 172.168.33.44;# 允许访问的ip地址,可以为all
}
}