软件:nginx

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
软件:nginx [2025/10/21 18:56] admin软件:nginx [2025/11/21 23:54] (当前版本) admin
行 25: 行 25:
 模块化设计:Nginx的核心功能相对简单,大部分功能是通过模块实现的。这些模块可以根据需要加载或卸载,提供了高度的灵活性。 模块化设计:Nginx的核心功能相对简单,大部分功能是通过模块实现的。这些模块可以根据需要加载或卸载,提供了高度的灵活性。
  
-部署+===== 部署 =====
  
 #安装Nginx服务器 #安装Nginx服务器
  
-<code -+<code nginx
-#卸载apache2+#卸载apache
 apt purge apache2 apache2-bin apache2-data apache2-utilsi apt purge apache2 apache2-bin apache2-data apache2-utilsi
 +
 #安装Nginx环境依赖 #安装Nginx环境依赖
 apt -y install curl gnupg2 ca-certificates lsb-release debian-archive-keyring apt -y install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
行 40: 行 41:
 systemctl enable nginx systemctl enable nginx
  
 +#使用浏览器访问localhost或ip访问成功
 +http://127.0.0.1/
  
-#使用浏览器访问localhost或ip访问成功 
 apt -y install php php-fpm apt -y install php php-fpm
-grep '^listen =' /etc/php/8.2/fpm/pool.d/www.conf +grep '^listen =' /etc/php/*/fpm/pool.d/www.conf 
- +如果返回sock文件地址说明默认监听sock文件 
-如果返回sock文件地址说明默认监听sock文件   +listen = /run/php/php*-fpm.sock 
-listen = /run/php/php8.2-fpm.sock   +如果返回IP:port 说明默认监听本地IP端口   
-如果返回IP:port 说明默认监听本地IP端口   +listen = 127.0.0.1:9000 
-listen = 127.0.0.1:9000   +nginx的ffastcgi_pass `<listen>` 编写规则,sock必须加unix:   
-nginx的ffastcgi_pass `<listen>` 编写规则,sock必须加unix:   +fastcgi_pass  unix:/run/php/php8.4-fpm.sock;  
-fastcgi_pass  unix:/run/php/php8.2-fpm.sock;  +
 fastcgi_pass  127.0.0.1:9000;   fastcgi_pass  127.0.0.1:9000;  
  
行 64: 行 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://$host$request_uri;         return 301 https://$host$request_uri;
     }     }
行 71: 行 73:
 sudo vi /etc/nginx/conf.d/default.conf sudo vi /etc/nginx/conf.d/default.conf
 server { server {
-    listen       433;+    listen  [::]:433 ssl http2; # IPv6 
 +    listen       433 ssl http2;
     server_name  localhost;     server_name  localhost;
 +    
 +    #填写证书文件绝对路径
 +    ssl_certificate /etc/nginx/conf.d/cert/localhost.pem;
 +    #填写证书私钥文件绝对路径
 +    ssl_certificate_key /etc/nginx/conf.d/cert/localhost.key;
 +
     root /var/www/html;     root /var/www/html;
     location / {     location / {
行 89: 行 98:
         root   /usr/share/nginx/html;         root   /usr/share/nginx/html;
     }     }
 +}
 +
 +location = /adminer.php{
 +    root /var/www/html;
 +    try_files $uri =404;
 +    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
 +    include fastcgi_params;
 +    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 } }
  
行 104: 行 121:
 #重启 #重启
 sudo systemctl restart nginx sudo systemctl restart nginx
- 
- 
  
  
行 132: 行 147:
  
 ===== SSL ===== ===== SSL =====
 +
 +==== 自生成测试 ====
 +
 +<code ->
 +apt -y install openssl 
 +
 +openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
 +    -keyout localhost.key \
 +    -out localhost.pem \
 +    -subj "/C=CN/ST=Beijing/L=Beijing/O=MyOrg/CN=localhost"
 +</code>
 +
 +-x509:生成自签名证书(不是 CSR)
 +
 +-nodes:不加密私钥(Nginx 启动时不需要输密码)
 +
 +-days 365:有效期 365 天(可改)
 +
 +-newkey rsa:2048:生成 2048 位 RSA 私钥
 +
 +-keyout localhost.key:私钥输出文件
 +
 +-out localhost.pem:证书输出文件(PEM 格式)
 +
 +-subj "...":自动填写证书信息(避免交互)
 +
 +-subj 字段含义(按需修改):
 +
 +字段 含义 示例
 +
 +C 国家(2字母) CN
 +
 +ST 省份 Beijing
 +
 +L 城市 Dongcheng
 +
 +O 组织/公司 MyCompany
 +
 +CN 通用名称(必须匹配访问的域名或 IP) localhost 或 127.0.0.1
 +
 +<code ->
 +chmod 600 localhost.key
 +chmod 644 localhost.pem
 +mkdir /etc/nginx/conf.d/cert
 +mv localhost.* /etc/nginx/conf.d/cert/
 +</code>
 +
 +自签名 SSL 证书在浏览器中一定会提示“不安全”,这是正常且预期的行为。自签名证书是任何人都可以生成,无法验证网站身份,存在中间人攻击风险。证书不是由受信任的 CA(证书颁发机构)签发的
 +
 +Chrome / Edge:在警告页面点击 “高级” → “继续前往...(不安全)”
 +
 +或直接在页面聚焦时输入:thisisunsafe(无提示,直接生效!)
 +
 +Firefox:点击 “高级” → “接受风险并继续”
 +
 +==== 生产环境 ====
  
 登录数字证书管理服务控制台 登录数字证书管理服务控制台
行 155: 行 226:
 #将证书文件和私钥文件上传到Nginx服务器的证书目录 #将证书文件和私钥文件上传到Nginx服务器的证书目录
  
-mv 17312297_sujj.wiki_nginx.zip /etc/nginx/conf.d/cert/+mv localhost_nginx.zip /etc/nginx/conf.d/cert/
  
 #解压 #解压
  
-unzip /etc/nginx/conf.d/cert/17312297_sujj.wiki_nginx.zip+unzip /etc/nginx/conf.d/cert/localhost_nginx.zip
  
 #配置 #配置
行 171: 行 242:
  
 #填写证书文件绝对路径 #填写证书文件绝对路径
-ssl_certificate /etc/nginx/conf.d/cert/sujj.wiki.pem;+ssl_certificate /etc/nginx/conf.d/cert/localhost.pem;
 #填写证书私钥文件绝对路径 #填写证书私钥文件绝对路径
-ssl_certificate_key /etc/nginx/conf.d/cert/sujj.wiki.key;+ssl_certificate_key /etc/nginx/conf.d/cert/localhost.key; 
 + 
 +#安全配置
 ssl_session_cache shared:SSL:1m; ssl_session_cache shared:SSL:1m;
 ssl_session_timeout 5m;  ssl_session_timeout 5m; 
行 182: 行 255:
 #表示优先使用服务端加密套件。默认开启 #表示优先使用服务端加密套件。默认开启
 ssl_prefer_server_ciphers on; ssl_prefer_server_ciphers on;
- 
-#将80重定向https 
-server { 
-    listen 80; 
-    #填写证书绑定的域名 
-    server_name <YOURDOMAIN>; 
-    #将所有HTTP请求通过rewrite指令重定向到HTTPS。 
-    rewrite ^(.*)$ https://$host$1; 
-    location / { 
-        index index.php index.html index.htm; 
-    } 
-} 
 </code> </code>
  
  • 软件/nginx.1761044162.txt.gz
  • 最后更改: 2025/10/21 18:56
  • admin