Skip to content

Nginx ​

RHEL ​

在线安装 ​

bash
# 查看Nginx信息
sudo yum info nginx
# 安装
sudo yum install nginx -y
# 安装最新版本
sudo yum install -y https://nginx.org/packages/rhel/9/x86_64/RPMS/nginx-1.28.0-1.el9.ngx.x86_64.rpm
sudo yum install -y https://nginx.org/packages/rhel/8/x86_64/RPMS/nginx-1.28.0-1.el8.ngx.x86_64.rpm
sudo yum install -y https://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.28.0-1.el7.ngx.x86_64.rpm

编译安装 ​

bash
# GCC
yum install -y gcc-c++
# PCRE
yum install -y pcre pcre-devel
# Zlib
yum install -y zlib zlib-devel
# OpenSSL
yum install -y openssl openssl-devel
# 解压缩
tar -zxvf nginx-1.28.0.tar.gz -C temp
# 进入源码目录
cd nginx-1.28.0
# 配置
# --prefix 安装目录
# --http_ssl_module 启用 HTTPS 协议
# --http_stub_status_module 启用状态监控支持
# --http_gzip_static_module 启用 GZip 支持
# --with-stream 启用 TCP 代理
./configure --prefix=/data/app/tools/nginx-proxy --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --without-http_rewrite_module
# 编译安装
make & make install

安装目录 ​

|---data                    --
|---|---app                 -- 应用目录
|---|---|---tools           -- 数据目录
|---|---|---|---nginx       -- Nginx
|---|---|---|---|---logs    -- Nginx数据目录
|---|---|---|---|---ssl     -- Nginx日志目录
|---|---|---temp            -- 临时目录

初始化 ​

初始化数据目录并授权。

bash
# 新建数据目录和日志目录
mkdir -p /data/app/tools/nginx/ssl
mkdir -p /data/app/tools/nginx/logs
# 赋予目录权限
chown -R nginx.nginx /data/app/tools/nginx

系统服务 ​

bash
sudo systemctl enable nginx.service
sudo systemctl disable nginx.service
sudo systemctl start nginx.service
sudo systemctl restart nginx.service
sudo systemctl reload nginx.service
sudo systemctl stop nginx.service
sudo systemctl status nginx.service

MacOS - 在线安装 ​

bash
# 安装
brew install nginx
# 启动服务
brew services start nginx
# 重启服务
brew services restart nginx
# 停止服务
brew services stop nginx
# 查看目录
brew info nginx

Ubuntu - 在线安装 ​

bash
# 安装
sudo apt install nginx

常用命令 ​

bash
# 启动
nginx
# 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
nginx -s stop
# 此方式停止步骤是待nginx进程处理任务完毕进行停止。
nginx -s quit
# 修改配置文件nginx.conf后,用此方式能让配置生效而不重启
nginx -s reload
# 查看路径
ps aux|grep nginx
# 查看
nginx -V
# 查看配置文件并检查配置文件是否有效
nginx -t
# 查看进程
ps -ef | grep nginx
# 检查端口
netstat -ntlp
# 检查端口
netstat -tulpn | grep :80

常用配置 - GZip ​

bash
# Gzip
# 是否启用
gzip on;
# 小于设置值的文件将不会压缩
gzip_min_length 1k;
# 设置gzip压缩针对的HTTP协议版本
gzip_http_version 1.0;
# 禁用低版本IE的Gzip压缩
gzip_disable "MSIE [1-6]\.";
# 设置压缩所需要的缓冲区大小
gzip_buffers 16 8k;
# 压缩级别,越大压缩越好也越占用CPU
gzip_comp_level 6;
# 无条件压缩所有结果数据
gzip_proxied any;
# MIME
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss application/javascript text/javascript application/json;

常用配置 - MacOS - PHP ​

nginx
server {
    listen 8888;
    server_name localhost;
    #
    root /Users/elvea/Workspace/github/platform-lite/public;
    index index.php;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    error_page 404 /index.php;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

常用配置 - SSL ​

bash
# SSL
ssl on;
ssl_certificate /data/app/tools/ssl/server.crt;
ssl_certificate_key /data/app/tools/ssl/server.key;
# 客户端能够重复使用存储在缓存中的会话参数时间
ssl_session_timeout 5m;
# 指定使用的ssl协议
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 指定许可的密码描述
ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!aNULL:!eNULL;
# SSLv3和TLSv1协议的服务器密码需求优先级高于客户端密码
ssl_prefer_server_ciphers on;

常用配置 - Basic Auth ​

安装密码生成工具

bash
sudo yum -y install httpd-tools

进入密码保存目录

bash
cd /data/app/tools/nginx/auth

生成密码

bash
htpasswd -c pass username

修改配置

auth_basic "请输入账号密码";
auth_basic_user_file /data/app/tools/nginx/auth/pass;

测试

bash
# 抓取内容
curl -u username:password URL
# 文件下载
wget --http-user=username --http-passwd=password https://host/file

常用配置 - Certbot && Let's Encrypt ​

安装

bash
# Ubuntu
sudo apt update
sudo apt install certbot
# RHEL
sudo dnf -y install epel-release
sudo dnf -y install certbot python3-certbot-nginx
# MacOS
brew install certbot

查看版本号

bash
certbot --version
bash
sudo certbot certonly
sudo certbot --nginx
# 查看当前证书
certbot certificates
# 续期
certbot renew