Skip to content

nginx

webdes
官网GitHub
zlib
pcre

Yum安装

sh
yum install epel-release

yum install -y openssl openssl-devel

yum install -y nginx

# list nginx package
rpm -ql nginx

源码编译安装

1.下载并解压nginx源码包

安装构建工具和依赖项:

sh
#yum groupinstall "Development Tools" 命令可以一次性安装多个开发工具,这些工具通常包括编译器、调试工具、文本编辑器等,能够满足大多数开发需求。具体安装的开发工具组可能因不同的Linux发行版而有所不同,但通常包含以下内容:
#‌编译器‌:如gcc、g++等,用于编译C/C++程序。
#‌调试工具‌:如gdb,用于调试程序。
#‌文本编辑器‌:如vim或emacs,用于编写代码。
#‌其他工具‌:如make、autoconf等,用于构建和配置软件。
# 安装c编译器
yum groupinstall "Development Tools"

yum install -y wget make gcc gcc-c++ openssl openssl-devel pcre-devel geoip-devel zlib-devel

# 根据模块选择安装
yum install -y openssl openssl-devel pcre-devel geoip-devel zlib-devel gd gd-devel

2.创建nginx用户

创建了一个名为 nginx 的用户,并将其登录 shell 设置为 /sbin/nologin,即无法登录系统,主要用于编译安装 nginx):

bash
useradd -r -s /sbin/nologin -M nginx

3.编译并安装nginx

指定安装路径,这里安装到/usr/local/nginx

sh
# 注意 --sbin-path --conf-path --pid-path的特殊配置 https://nginx.org/en/docs/configure.html
./configure \
  --prefix=/usr/local/nginx \ #指定安装目录
  --user=nginx \ #运行NGINX的worker子进程的属主
  --group=nginx \ #运行NGINX的worker子进程的属组
  
  # 不指定则会一--prefix的目录相对建目录
  --sbin-path=/opt/nginx/sbin/nginx \ 
  --conf-path=/opt/nginx/conf/nginx.conf \
  --pid-path=/opt/nginx/pid/nginx.pid \
  --error-log-path=/opt/nginx/logs/error.log \
  --http-log-path=/opt/nginx/logs/nginx.log \
  
  
  --with-pcre=../pcre2-10.39 #支持正则表达式的库的路径,下载pcre第三方库,放在制定目录下即可
  --with-zlib=../zlib-1.3 # zlib库的存放路径,gzip模块会用到
  
  #--with 默认不内置,显示加上,--without 默认内置,显示去掉
  --with-compat \
  --with-file-aio \
  --with-threads \
  --with-http_addition_module \
  --with-http_auth_request_module \
  --with-http_dav_module \
  --with-http_flv_module \
  --with-http_gunzip_module \
  --with-http_gzip_static_module \
  --with-http_mp4_module \ 
  --with-http_random_index_module \
  --with-http_realip_module \
  --with-http_secure_link_module \
  --with-http_slice_module \
  --with-http_ssl_module \
  --with-http_stub_status_module \
  --with-http_sub_module \
  --with-http_v2_module \
  --with-stream \
  --with-stream_realip_module \
  --with-stream_ssl_module \
  --with-stream_ssl_preread_module \
  --with-stream_geoip_module \
  --with-mail \
  --with-mail_ssl_module
   
    
# 输出配置检查结果
Configuration summary
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

练习使用

sh
 # --with-http_image_filter_module需要gd gd-devel库
./configure \
    --prefix=/opt/nginx \
    --user=nginx \
    --group=nginx \
    --conf-path=/opt/nginx/conf/nginx.conf \
    --pid-path=/opt/nginx/pid/nginx.pid \
    --error-log-path=/opt/nginx/logs/error.log \
    --http-log-path=/opt/nginx/logs/nginx.log \
    --sbin-path=/opt/nginx/sbin/nginx \
    --with-pcre=/opt/source/pcre2-10.45 \
    --with-zlib=/opt/source/zlib-1.3.1 \
    --with-http_ssl_module \
    --with-http_image_filter_module \
    --with-http_stub_status_module

开始编译nginx源码并安装nginx

bash
# 并行编译程序
make -j $(nproc)
make install
创建nginx软链接
bash
ln -s /opt/nginx/sbin/nginx /usr/bin/nginx

4.验证nginx版本

进入nginx安装目录,执行以下命令验证:

sh
./sbin/nginx -V

# 显示版本信息
nginx version: nginx/1.21.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_geoip_module --with-mail --with-mail_ssl_module

5.临时启动Nginx

sh
/usr/local/nginx/sbin/nginx -t  		# 检查配置文件是否正确
/usr/local/nginx/sbin/nginx 			# 启动 Nginx
/usr/local/nginx/sbin/nginx -s quit		# 优雅地停止 Nginx,然后手动启动
/usr/local/nginx/sbin/nginx -s reload	# 重载nginx


# 如创建了软链接则可以使用软连接启动 ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
/usr/bin/nginx -s quit   	# 优雅地停止 Nginx,然后手动启动
/usr/bin/nginx           	# 启动 Nginx
/usr/bin/nginx -s reload  	# 先尝试重载配置,如果失败再重新启动
/usr/bin/nginx -t			# 检查配置文件是否正确

三、配置Nginx

配置启动服务

创建一个名为 nginx.service 的 systemd 服务单元文件,用于管理 Nginx 服务,存放于/etc/systemd/system目录下,并添加以下内容:

sh
cat > /etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target

EOF

提示

服务单元文件配置详解:

这是一个 systemd 服务单元文件,用于管理和配置Nginx 服务。systemd 是一个 Linux 系统的初始化系统和服务管理器,它管理和配置系统启动后运行的服务。这个文件的主要部分包括 UnitServiceInstall。

[Unit] 部分包含了服务的描述和依赖关系: Description:服务的描述,这里是 “The NGINX HTTP and reverse proxy server”。 After:这个指定了服务的启动顺序。这里,Nginx 会在 network.target(网络服务)、remote-fs.target(远程文件系统)和 nss-lookup.target(名字服务解析)之后启动。 [Service] 部分定义了服务的启动、停止和重载的命令以及其他相关设置: Type=forking:这意味着这个服务会创建一个或多个子进程。 ExecStart:启动服务的命令。 ExecReload:重新加载服务的命令。 ExecStop:停止服务的命令。 PrivateTmp=true:这是一个安全选项,如果设置为 true,则此服务将有一个独立的临时目录,与其他服务和用户隔离。 [Install] 部分用于定义如何 ‘安装’ 这个服务: WantedBy:这个指定了哪个目标需要这个服务。这里,multi-user.target 是一个常见的目标,它表示多用户环境。

启动nginx服务

配置完成后,启动Nginx服务:

sh
#重载nginx配置
systemctl daemon-reload			
#启动
systemctl start nginx
# 查看Nginx服务状态
systemctl status nginx
# 设置开机自启服务
systemctl enable nginx
# 重启服务
systemctl restart nginx

使用nginx

1.配置nginx反向代理示例

假设需要配置一个通过SSL域名去反向代理,在 /usr/local/nginx/conf/conf.d 目录下创建一个虚拟主机配置文件,其内容如下:

sh
server {
    listen 443 ssl http2;
    server_name searx.jerion.cn;

    ssl_certificate /etc/ssl/certs/searx.jerion.cn/searx.jerion.cn.pem;
    ssl_certificate_key /etc/ssl/certs/searx.jerion.cn/searx.jerion.cn.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

    location / {
        proxy_pass http://10.22.51.65:7780;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

在 Nginx 配置中使用 server_tokens off; 语句可以隐藏 HTTP 头部中的 Nginx 版本号,这是一个提高安全性的措施,防止攻击者通过版本号识别潜在的漏洞。

下面是如何在 Nginx 配置中使用 server_tokens off; 的步骤:

  1. 打开 Nginx 配置文件:通常配置文件位于 /etc/nginx/nginx.conf/etc/nginx/conf.d/ 目录下,但具体位置可能根据你的安装而有所不同。

  2. 编辑配置文件:找到 http 块,然后添加 server_tokens off; 语句。例如:

    sh
    http {
        server_tokens off;
    
        # 其他配置项
    }

相关问题

**systemctl reload nginxsystemctl restart nginxnginx -s reload:**这三条命令都用于管理和控制 Nginx 服务,但它们有不同的作用和使用场景。以下是每条命令的详细解释及其显著区别:

1.详细解释

systemctl reload nginx

  • 作用:重新加载 Nginx 服务。
  • 描述:该命令会通知 Nginx 重新加载其配置文件,而不停止服务。这意味着任何新的配置更改将被应用,但现有的连接不会被中断。
  • 使用场景:当你对 Nginx 的配置文件做了更改,并希望应用这些更改而不中断当前连接时使用。

systemctl restart nginx

  • 作用:重启 Nginx 服务。
  • 描述:该命令会停止 Nginx 服务并立即重新启动它。这会导致所有当前连接中断。
  • 使用场景:当你需要完全重新启动 Nginx 服务,比如安装了新的模块或补丁,或需要解决一些服务异常问题时使用。

nginx -s reload

  • 作用:使用 Nginx 自带的控制信号重新加载配置。
  • 描述:该命令发送 SIGHUP 信号给 Nginx 主进程,告诉它重新加载配置文件。这与 systemctl reload nginx 的效果相同,即应用新的配置而不中断服务。
  • 使用场景:与 systemctl reload nginx 相同,当你对 Nginx 配置文件进行了修改并希望应用这些更改而不中断当前连接时使用。

2.区别总结

  • systemctl reload nginxnginx -s reload 都用于重新加载 Nginx 配置文件,但 systemctl reload nginx 是通过 systemd 来管理服务的,而 nginx -s reload 是直接向 Nginx 进程发送信号。

  • systemctl restart nginx 用于完全重启 Nginx 服务,会导致服务中断,适用于需要彻底重启服务的情况。

  • nginx -s reloadsystemctl reload nginx 都不会中断当前连接,而 systemctl restart nginx 会中断所有连接。

3.推荐使用场景

  • 应用配置更改systemctl reload nginxnginx -s reload
  • 完全重启服务systemctl restart nginx