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.24.0-1.el9.ngx.x86_64.rpm
sudo yum install -y https://nginx.org/packages/rhel/8/x86_64/RPMS/nginx-1.24.0-1.el8.ngx.x86_64.rpm
sudo yum install -y https://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.24.0-1.el7.ngx.x86_64.rpm
# 查看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.24.0-1.el9.ngx.x86_64.rpm
sudo yum install -y https://nginx.org/packages/rhel/8/x86_64/RPMS/nginx-1.24.0-1.el8.ngx.x86_64.rpm
sudo yum install -y https://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.24.0-1.el7.ngx.x86_64.rpm

RHEL - 编译安装

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.24.0.tar.gz -C temp
# 进入源码目录
cd nginx-1.24.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
# 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.24.0.tar.gz -C temp
# 进入源码目录
cd nginx-1.24.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

MacOS - 在线安装

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

Ubuntu - 在线安装

bash
# 安装
sudo apt install nginx
# 安装
sudo apt install nginx

RHEL - 系统服务

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
# 随系统启动
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

常用命令

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
# 启动
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;
# 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;
    }
}
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;
# 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
sudo yum -y install httpd-tools

进入密码保存目录

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

生成密码

bash
htpasswd -c pass username
htpasswd -c pass username

修改配置

auth_basic "请输入账号密码";
auth_basic_user_file /data/app/tools/nginx/auth/pass;
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
# 抓取内容
curl -u username:password URL
# 文件下载
wget --http-user=username --http-passwd=password https://host/file