差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
| 软件:nginx [2025/10/21 15:44] – admin | 软件:nginx [2025/11/21 23:54] (当前版本) – admin | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== Nginx ====== | ||
| + | |||
| HTTP服务器:Nginx可以作为独立的Web服务器来托管网站和应用程序。 | HTTP服务器:Nginx可以作为独立的Web服务器来托管网站和应用程序。 | ||
| 行 23: | 行 25: | ||
| 模块化设计:Nginx的核心功能相对简单,大部分功能是通过模块实现的。这些模块可以根据需要加载或卸载,提供了高度的灵活性。 | 模块化设计:Nginx的核心功能相对简单,大部分功能是通过模块实现的。这些模块可以根据需要加载或卸载,提供了高度的灵活性。 | ||
| - | 部署 | + | ===== 部署 |
| # | # | ||
| - | < | + | < |
| - | #卸载apache2 | + | #卸载apache |
| apt purge apache2 apache2-bin apache2-data apache2-utilsi | apt purge apache2 apache2-bin apache2-data apache2-utilsi | ||
| + | |||
| # | # | ||
| apt -y install curl gnupg2 ca-certificates lsb-release debian-archive-keyring | apt -y install curl gnupg2 ca-certificates lsb-release debian-archive-keyring | ||
| 行 38: | 行 41: | ||
| systemctl enable nginx | systemctl enable nginx | ||
| - | # | + | # |
| + | http:// | ||
| + | |||
| + | apt -y install php php-fpm | ||
| + | grep ' | ||
| + | # 如果返回sock文件地址说明默认监听sock文件 | ||
| + | listen = / | ||
| + | # 如果返回IP: | ||
| + | listen = 127.0.0.1: | ||
| + | # nginx的ffastcgi_pass `< | ||
| + | fastcgi_pass | ||
| + | fastcgi_pass | ||
| # | # | ||
| 行 50: | 行 65: | ||
| # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response. | # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response. | ||
| location / { | location / { | ||
| + | # $host保留原始Host头,$request_uri保留路径和参数 | ||
| return 301 https:// | return 301 https:// | ||
| } | } | ||
| 行 57: | 行 73: | ||
| sudo vi / | sudo vi / | ||
| server { | server { | ||
| - | listen | + | |
| + | | ||
| server_name | server_name | ||
| - | | + | |
| + | # | ||
| + | ssl_certificate | ||
| + | # | ||
| + | ssl_certificate_key /etc/nginx/ | ||
| + | |||
| + | root /var/www/html; | ||
| location / { | location / { | ||
| index index.php index.html index.htm; | index index.php index.html index.htm; | ||
| } | } | ||
| location ~ .php$ { | location ~ .php$ { | ||
| - | root /usr/share/nginx/html; | + | root /var/www/html; |
| fastcgi_pass < | fastcgi_pass < | ||
| fastcgi_index index.php; | fastcgi_index index.php; | ||
| 行 70: | 行 93: | ||
| include fastcgi_params; | include fastcgi_params; | ||
| } | } | ||
| + | # error_page | ||
| error_page | error_page | ||
| location = /50x.html { | location = /50x.html { | ||
| root / | root / | ||
| } | } | ||
| + | } | ||
| + | |||
| + | location = / | ||
| + | root / | ||
| + | try_files $uri =404; | ||
| + | fastcgi_pass unix:/ | ||
| + | include fastcgi_params; | ||
| + | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
| } | } | ||
| 行 79: | 行 111: | ||
| # 把include / | # 把include / | ||
| # include / | # include / | ||
| + | 按`esc`键,输入`: | ||
| - | + | 在终端执行命令 | |
| - | #修改root | + | #配置是否正确 |
| - | root /var/www/html/ | + | nginx -t |
| + | # | ||
| + | #nginx: the configuration file /etc/nginx/nginx.conf syntax is ok | ||
| + | #nginx: configuration file /etc/nginx/nginx.conf test is successful | ||
| #重启 | #重启 | ||
| sudo systemctl restart nginx | sudo systemctl restart nginx | ||
| 行 110: | 行 146: | ||
| </ | </ | ||
| - | SSL | + | ===== SSL ===== |
| + | |||
| + | ==== 自生成测试 ==== | ||
| + | |||
| + | <code -> | ||
| + | apt -y install openssl | ||
| + | |||
| + | openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ | ||
| + | -keyout localhost.key \ | ||
| + | -out localhost.pem \ | ||
| + | -subj "/ | ||
| + | </ | ||
| + | |||
| + | -x509:生成自签名证书(不是 CSR) | ||
| + | |||
| + | -nodes:不加密私钥(Nginx 启动时不需要输密码) | ||
| + | |||
| + | -days 365:有效期 365 天(可改) | ||
| + | |||
| + | -newkey rsa: | ||
| + | |||
| + | -keyout localhost.key:私钥输出文件 | ||
| + | |||
| + | -out localhost.pem:证书输出文件(PEM 格式) | ||
| + | |||
| + | -subj " | ||
| + | |||
| + | -subj 字段含义(按需修改): | ||
| + | |||
| + | 字段 含义 示例 | ||
| + | |||
| + | C 国家(2字母) CN | ||
| + | |||
| + | ST 省份 Beijing | ||
| + | |||
| + | L 城市 Dongcheng | ||
| + | |||
| + | O 组织/ | ||
| + | |||
| + | CN 通用名称(必须匹配访问的域名或 IP) localhost 或 127.0.0.1 | ||
| + | |||
| + | <code -> | ||
| + | chmod 600 localhost.key | ||
| + | chmod 644 localhost.pem | ||
| + | mkdir / | ||
| + | mv localhost.* / | ||
| + | </ | ||
| + | |||
| + | 自签名 SSL 证书在浏览器中一定会提示“不安全”,这是正常且预期的行为。自签名证书是任何人都可以生成,无法验证网站身份,存在中间人攻击风险。证书不是由受信任的 CA(证书颁发机构)签发的 | ||
| + | |||
| + | Chrome / Edge:在警告页面点击 “高级” → “继续前往...(不安全)” | ||
| + | |||
| + | 或直接在页面聚焦时输入:thisisunsafe(无提示,直接生效!) | ||
| + | |||
| + | Firefox:点击 “高级” → “接受风险并继续” | ||
| + | |||
| + | ==== 生产环境 ==== | ||
| 登录数字证书管理服务控制台 | 登录数字证书管理服务控制台 | ||
| 行 119: | 行 211: | ||
| 在立即购买面板,保持默认选项,仔细阅读并勾选服务协议,单击立即购买并完成支付 | 在立即购买面板,保持默认选项,仔细阅读并勾选服务协议,单击立即购买并完成支付 | ||
| + | |||
| + | 阿里云-> | ||
| + | |||
| + | 在终端执行命令 | ||
| + | |||
| + | # | ||
| + | |||
| + | nginx -t | ||
| + | |||
| + | # | ||
| + | |||
| + | mkdir / | ||
| + | |||
| + | # | ||
| + | |||
| + | mv localhost_nginx.zip / | ||
| + | |||
| + | #解压 | ||
| + | |||
| + | unzip / | ||
| + | |||
| + | #配置 | ||
| + | |||
| + | vi / | ||
| + | |||
| + | 修改default.conf文件 | ||
| + | |||
| + | <code -> | ||
| + | listen 443 ssl; | ||
| + | |||
| + | # | ||
| + | ssl_certificate / | ||
| + | # | ||
| + | ssl_certificate_key / | ||
| + | |||
| + | # | ||
| + | ssl_session_cache shared: | ||
| + | ssl_session_timeout 5m; | ||
| + | # | ||
| + | # | ||
| + | ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256: | ||
| + | ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; | ||
| + | # | ||
| + | ssl_prefer_server_ciphers on; | ||
| + | </ | ||
| + | |||
| + | 按`esc`键,输入`: | ||