nginx安装
通过包管理器安装
# ubuntu
sudo apt update
sudo apt install nginx
编译安装: 最灵活,可以安装特殊模块和修改源码
./configure # 预编译配置,输出安装的路径
make # 编译
make install # 编译安装
容器安装
docker pull nginx:version
服务器的启动停止
nginx启动后会作为后台程序运行,需要通过ps查找该进程,一般nginx会启动master和work两个进程,因为nginx的master负责管理配置文件和管理work进程,work进程负责处理请求, 属于一主多从
启动指令: nginx
查看nginx的启动情况: ps | grep nginx
查看端口占用情况: lsof -i:80
nginx的其他指令
- 优雅停止:
nginx -s quit - 立即停止:
nginx -s stop - 重载配置文件:
nginx -s reload - 重新打开日志文件:
nginx -s reopen - 查看版本信息安装路径等信息:
nginx -Vnginx version: nginx/1.24.0 built with OpenSSL 3.0.8 7 Feb 2023 (running with OpenSSL 3.1.1 30 May 2023) TLS SNI support enabled configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf # 配置文件位置 --sbin-path=/usr/bin/nginx # 可执行文件目录 --pid-path=/run/nginx.pid --lock-path=/run/lock/nginx.lock # 锁文件 --user=http # 用户 --group=http # 组 --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --http-client-body-temp-path=/var/lib/nginx/client-body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-cc-opt='-march=x86-64-mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -g -ffile-prefix-map=/build/nginx/src=/usr/src/debug/nginx -flto=auto' --with-ld-opt='-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto=auto' --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_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-mail --with-mail_ssl_module --with-pcre-jit --with-stream --with-stream_geoip_module --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads
静态站点部署
nginx的安装位置
$: nginx -t 检查配置文件是否正确
# 常见的位置
/etc/nginx/conf
/usr/local/etc/nginx
/opt/homebrew/etc/nginx
nginx配置组成
worker_process 1; # 工作进程数量,一般设置成auto,这样nginx就可以根据cpu数量设置工作进程数量
events { # 服务器和客户端之间网络连接的一些配置
worker_connections 1024; # 每个进程可以接收多少个网络连接
}
http { # 虚拟主机,反向代理等都在这里配置
server { # 虚拟主机
}
include mime.types; # 将mime.types这个文件包含进来,该文件包含了多个文件的处理方式
include servers/*; # 将servers目录下所有的配置文件都包含进来
}
nginx反向代理
正向代理: 代理客户端,如浏览器访问google,需要vpn 反向代理: 代理服务器,浏览器访问服务端,请求首选转发到nginx,nginx根据配置实现服务转发到相应的服务端
反向代理配置
upstream backend { # 反向代理配置
ip_hash; # 策略根据客户端ip地址进行hash,同一个客户端的请求会被放到同一个服务器上,这样就解决了session相关的问题
server 127.0.0.1:xxx ; # 服务器的ip和端口
server 127.0.0.1:xxx ; # 服务器的ip和端口
server 127.0.0.1:xxx ; # 服务器的ip和端口
server 127.0.0.1:xxx weight=3; # 服务器的ip和端口,该服务的权重是其他服务的3倍
}
server {
listen 80;
location /app {
proxy_pass http://backend; # 将请求代理到 backend中
}
}
https配置
https是在ssl协议上增加了ssl证书.
使用https生成证书
- 生成私钥文件(private key)
openssl genrsa -out private.key 2048 - 生成私钥生成证书签名请求文件 (CSR文件)
openssl req -new -key private,key -out cert.scr - 使用私钥对证书申请签名从而生成证书文件 (pem文件)
openssl x509 -req -in cert.csr -out cacert.pem -signkey private.key
nginx配置
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /.../cacert.pem # 证书文件名称
ssl_certificate_key /.../private.key # 证书私钥文件爱你名称
ssl_session_timeout 5m; # 缓存有效期
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # 安全连接可选的加密协议
ssl_ciphers 加密套件; # 配置加密套件,加密算法, 写法遵循openssl标准;
ssl_prefer_server_ciphers on; # 使用服务端的首选算法
}
# 配置80端口重定向到https
server {
listen 80;
return 301 https://$server_name$request_url;
}
虚拟主机
nginx中的虚拟主机是通过server块实现的,每个sever块都是一个虚拟主机服务