资讯详情 用心服务、精益求精、感恩遇见、合作共赢
phpstudy nginx 伪静态代码

以下是 PHPStudy 中常用的 Nginx 伪静态配置模板及使用方法:

---

### **通用伪静态配置(在 `server{}` 块内添加)**
```nginx
location / {
    try_files $uri $uri/ /index.php?$query_string;
    
    # 隐藏index.php
    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php/$1 last;
        break;
    }
}
```

---

### **常见框架伪静态规则**

#### **1. WordPress**
```nginx
location / {
    try_files $uri $uri/ /index.php?$args;
    
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
    rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
```

#### **2. Laravel**
```nginx
location / {
    try_files $uri $uri/ /index.php?$query_string;
    
    # 处理前端控制器
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
```

#### **3. ThinkPHP**
```nginx
location / {
    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php/$1 last;
    }
}
```

---

### **特殊场景规则**

#### **1. 强制HTTPS**
```nginx
if ($scheme = http) {
    return 301 https://$host$request_uri;
}
```

#### **2. 目录禁止访问**
```nginx
location ~ /(runtime|upload)/ {
    deny all;
    return 403;
}
```

---

### **操作步骤**
1. 打开 PHPStudy → 网站 → 对应站点 → 伪静态
2. 选择 `Nginx` 类型
3. 粘贴对应规则代码
4. 保存后重启 Nginx 服务

---

### **调试技巧**
1. 检查配置语法:`nginx -t`(在 PHPStudy 终端执行)
2. 查看错误日志:`logs/nginx_error.log`
3. 测试规则是否生效:访问不存在路径看是否重定向到首页

---

### **注意事项**
1. ThinkPHP需要同时开启 `pathinfo` 支持
2. 带参数的重定向要用 `last` 而非 `break`
3. 特殊字符需用 `~*` 进行正则匹配(如中文URL)

扫码识别