nginx学习笔记

# apt安装nginx apt install nginx

在基于debian的linux发行版中,nginx服务器的默认安装目录是/etc/nginx。这个目录包含了apache服务器的配置文件和相关资源。

. ├── conf.d(用于存放虚拟主机配置文件) ├── fastcgi.conf(FastCGI配置文件) ├── fastcgi_params(FastCGI参数配置文件) ├── koi-utf(用于字符编码转换) ├── koi-win(用于字符编码转换) ├── mime.types(定义了MIME类型及其对应的文件扩展名) ├── modules-available(存放nginx可用的模块) ├── modules-enabled(存放启用的nginx模块链接) ├── nginx.conf(nginx主配置文件) ├── proxy_params(用于定义反向代理服务器的参数配置) ├── scgi_params(用于定义SCGI服务器的参数配置) ├── sites-available(存放可用的虚拟主机配置文件) ├── sites-enabled(存放启用的虚拟主机配置文件链接) ├── snippets(可重用的nginx配置文件片段) ├── ssl(自己新建,存放SSL证书和秘钥文件) ├── uwsgi_params(用于定义uWSGI服务器的参数配置) └── win-utf(用于字符编码转换,一般不需要修改)

1、配置http

# 在sites-available文件夹下新建xxx.example.com.conf server { listen 80; # listen 443 ssl; server_name rssbridge1.gujiakai.top; # ssl_certificate /etc/nginx/ssl/rssbridge1.gujiakai.top.cert.pem; # ssl_certificate_key /etc/nginx/ssl/rssbridge1.gujiakai.top.key.pem; location / { proxy_pass http://127.0.0.1:3001; } } ln -s /etc/nginx/sites-available/xxx.example.com.conf /etc/nginx/sites-enabled/ # 记得在主配置文件nginx.conf中包含sites-enabled文件夹下的内容。 # 在http块中添加一条:include /etc/nginx/sites-enabled/*;

2、申请SSL证书

acme.sh --issue -d xxx.example.com --nginx # 安装证书 acme.sh --install-cert -d xxx.example.com \ --key-file /usr/local/nginx/ssl/private/xxx.example.com.key.pem \ --fullchain-file /usr/local/nginx/ssl/certs/xxx.example.com.cert.pem \ --reloadcmd "service nginx force-reload"
# 安装证书 acme.sh --install-cert -d uptime.gujiakai.top \ --key-file /etc/nginx/ssl/uptime.gujiakai.top.key.pem \ --fullchain-file /etc/nginx/ssl/uptime.gujiakai.top.cert.pem \ --reloadcmd "service nginx force-reload"

3、彻底卸载nginx

# 停止nginx服务 syetemctl stop nginx # 禁用nginx服务,使其不会在开机时自启 systemctl disable nginx # 移除nginx服务文件 rm /etc.systemd/system/nginx.service # 重新加载系统管理守护程序配置文件 systemctl daemon-reload # 完全删除nginx,并删除其所有配置和数据文件 apt purge nginx # 重置所有已经失败的系统单元(如服务、套接字、挂载点等)的状态,以便它们可以重新尝试启动。 systemctl reset-failed

4、典型的nginx配置反向代理模板

server { listen 80; server_name xxx.example.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name xxx.example.com; ssl_certificate /etc/nginx/ssl/xxx.example.com.cert.pem; ssl_certificate_key /etc/nginx/ssl/xxx.example.com.key.pem; location / { proxy_pass http://127.0.0.1:3001; } }
server { listen 80; server_name example.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/ssl/example.com.cert.pem; ssl_certificate_key /etc/nginx/ssl/example.com.key.pem; location / { proxy_pass http://127.0.0.1:8080; # 留客户端请求中的 "Host" 头。 proxy_set_header Host $http_host; # 保留客户端的真实 IP 地址 proxy_set_header X-Real-IP $remote_addr; # 保留请求经过的代理服务器及其客户端 IP 地址。 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 保留客户端请求使用的协议(HTTP 或 HTTPS)。 proxy_set_header X-Forwarded-Proto $scheme; # 保留用于支持协议升级的 "Upgrade" 头(例如从 HTTP/1.1 切换到 WebSocket)。 proxy_set_header Upgrade $http_upgrade; # 设置代理请求的 HTTP 版本为 1.1,以支持一些特定功能,如分块传输编码和协议升级。 proxy_http_version 1.1; } }

5、centos7安装nginx步骤

# 安装 EPEL 仓库 yum install epel-release # 使用 EPEL 仓库安装 Nginx 并在安装过程中自动确认(-y 参数) dnf install nginx -y # 启用 Nginx 服务,使其在系统启动时自动运行 systemctl enable nginx # 启动 Nginx 服务 systemctl start nginx

注:在 CentOS 7 中,默认的仓库里并没有包含 Nginx。要安装 Nginx,你需要先启用 EPEL(Extra Packages for Enterprise Linux)仓库,这是一个由 Fedora 项目提供的为 RHEL 和 CentOS 提供额外软件包的仓库。

6、angie web服务器是nginx的分支

基本操作同nginx

cd /etc/angie/http.d vim alist.conf server { listen 80; server_name alist.gujiakai.cn; location / { proxy_pass http://127.0.0.1:5244; proxy_set_header Host $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-Proto $scheme; } } systemctl reload angie curl https://get.acme.sh | sh -s email="gujiakai28@gmail.com" source ~/.bashrc vim ~/.bashrc export Ali_Key="<key>" export Ali_Secret="<secret>" source ~/.bashrc acme.sh --issue --dns dns_ali -d alist.gujiakai.cn mkdir -p /etc/ssl/angie chmod 700 /etc/ssl/angie acme.sh --install-cert -d alist.gujiakai.cn \ --key-file /etc/ssl/angie/alist.gujiakai.cn.key.pem \ --fullchain-file /etc/ssl/angie/alist.gujiakai.cn.cert.pem \ --reloadcmd "service angie force-reload" vim /etc/angie/http.d/alist.conf # 重定向HTTP到HTTPS server { listen 80; server_name alist.gujiakai.cn; return 301 https://$host$request_uri; } # HTTPS服务器配置 server { listen 443 ssl; server_name alist.gujiakai.cn; ssl_certificate /etc/ssl/angie/alist.gujiakai.cn.cert.pem; ssl_certificate_key /etc/ssl/angie/alist.gujiakai.cn.key.pem; # 强化SSL设置 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://127.0.0.1:5244; proxy_set_header Host $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-Proto $scheme; } } systemctl reload angie

7、angie安装console light

apt install angie-console-light vim /etc/angie/http.d/angie-console.conf