Nginx

本文详细介绍了如何在Linux系统上安装Nginx,包括通过官方存储库和编译安装的方法,以及如何配置location指令和try_files指令。同时,还涉及了如何使用HTTP基本身份验证对Nginx资源进行访问控制。
摘要由CSDN通过智能技术生成

文档

中文文档:https://blog.redis.com.cn/doc/index.html

系统存储库安装 Nginx

sudo yum install epel-release #为 Nginx 包安装 EPEL 存储库
sudo yum update #更新存储库
yum install nginx #安装 Nginx
sudo nginx -v #验证安装

编译安装Ngijnx

下载nginx

cd /usr/local #安装目录  

# 根据编译需求安装deb系列依赖项
apt-get install build-essential libpcre3 libpcre3-dev libgeoip-dev zlib1g zlib1g.dev libxslt-dev libgd-dev libatomic-ops-dev libperl-dev libssl-dev  

wget http://nginx.org/download/nginx-1.25.4.tar.gz #下载nginx
tar -zxvf 	nginx-1.25.4.tar.gz
cd nginx-1.20.1 

#deb系列编译模块参数
./configure --prefix=/usr/local/nginx --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-http_sub_module --with-pcre --with-pcre-jit --with-debug --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-libatomic  

make && make install  #安装

使用systemctl管理nginx配置文件

mkdir /etc/nginx 
cp -r /usr/local/nginx/conf/* /etc/nginx

编辑systemctl脚本

vi /usr/lib/systemd/system/nginx.service 
[Unit]                                                                                     
Description=nginx
After=network.target

[Service]                                                                                
Type=forking                                                                                                    
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf   
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf   
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                                 
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                                                                                       
PrivateTmp=true                                                                  

[Install]
WantedBy=multi-user.target 

刷新systemctl脚本

systemctl daemon-reload 

启动nginx并设置开机启动

systemctl start nginx && systemctl enable nginx 

连接nginx命令

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

浏览器访问ip验证,根目录为/usr/local/nginx/html

bash脚本编译安装nginx

vi /nginx.sh #创建
#!/bin/bash

# 常量设置
nginx_version="1.25.4" # nginx版本
install_path="/usr/local" # 安装、操作目录

# 安装依赖
echo "......正在安装依赖......"
apt-get install -y build-essential libpcre3 libpcre3-dev libgeoip-dev zlib1g zlib1g.dev libxslt-dev libgd-dev libatomic-ops-dev libperl-dev libssl-dev
echo "......依赖安装完成......"

# 下载nginx源码包
echo "......正在下载源码包......"
wget -P ${install_path} http://nginx.org/download/nginx-${nginx_version}.tar.gz
echo "......源码包下载完成......"

# 解压缩
echo "......正在解压缩源码包......"
cd ${install_path}
tar -zvxf ${install_path}/nginx-${nginx_version}.tar.gz ${install_path}
echo "......源码包解压缩完成......"

# 编译安装
echo "......正在编译安装......"
${install_path}/nginx-${nginx_version}/configure --prefix=/usr/local/nginx --with-select_module --with-poll_module --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-http_sub_module --with-pcre --with-pcre-jit --with-debug --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-libatomic
cd ${install_path}/nginx-${nginx_version} && make && make install
echo "......编译安装完成......"

# 复制配置文件
echo "......正在复制配置文件......"
mkdir /etc/nginx
cp -r ${install_path}/nginx/conf/* /etc/nginx
echo "......配置文件复制完成......"

# 配置systemctl脚本
echo "......正在配置systemctl脚本......"
cat>/usr/lib/systemd/system/nginx.service<<EOF
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf   
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf   
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                                 
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                                                                                       
PrivateTmp=true   

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start nginx
systemctl enable nginx
systemctl status nginx

echo "......systemctl脚本配置完成......"
echo "......!!!脚本运行完成!!!......"
sh /nginx.sh #运行

启动和重启 Nginx

#启动 Nginx
systemctl start nginx 
service start nginx #没有 systemd 的 Linux 发行版(低于 Redhat 7 的版本)
nginx -s start #源码编译安装的 nginx 启动命令
/etc/init.d/nginx start #较旧的 Ubuntu Linux 版本

#启用 Nginx 服务
systemctl enable nginx #开机启动 Nginx 服务
systemctl stop nginx #停止 Nginx
nginx -s stop

#重启 Nginx
systemctl restart nginx 

#重新加载 Nginx
systemctl reload nginx 
nginx -s reload

#测试 Nginx 配置
nginx -t

Nginx的location指令

语法

location [modifier] path

location 指令修饰符匹配优先级

=           - 完全匹配  
^~          - 优先匹配  
~ && ~*     - 正则表达式匹配  
no modifier - 前缀匹配 

示例

location  = / {
  # 只匹配请求 "/"
  [ configuration A ] 
}
location  / {
  # 匹配任何请求,因为所有请求都是以"/"开始
  # 但是更长字符匹配或者正则表达式匹配会优先匹配
  [ configuration B ] 
}
location /documents/ {
  # 匹配所有 /documents/ 开头的请求,在没有正则表达
  # 式匹配时选择该locaiton
  [ configuration C ]
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开始的请求,并停止匹配其它location
  [ configuration D ] 
}E
location ~* .(gif|jpg|jpeg)$ {
  # 匹配以 gif, jpg, or jpeg结尾的请求. 
  # 但是所有 /images/ 目录的请求将由 [Configuration D]处理.   
  [ configuration E ] 
}

请求URI例子:

/ -> 匹配A
/index.html -> 匹配B
/documents/a.html -> 匹配C
/images/1.gif -> 匹配D
/documents/1.jpg -> 匹配E

try_files 指令

该指令尝试不同的路径,并将返回找到的任何一个。

server {  
  location / {  
    try_files $uri /index.html =404;  
  }  
}  

Nginx 安全控制

使用 HTTP 基本身份验证限制访问

通过实施用户名和密码身份验证来限制对我们网站或其某些部分的访问,用户名和密码取自密码文件创建工具 apache2-utils

安装apache2-utils

apt-get install -y apache2-utils

创建密码文件和用户

htpasswd -c /etc/nginx/.htpasswd user1  

Nginx HTTP 基本身份验证

location /api {  
    auth_basic           "Administrator's Area";  
    auth_basic_user_file /etc/nginx/.htpasswd;   
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值