112 nginx去掉index.php 只需2个步骤

为了使URL更简洁优雅些,我们可以通过Nginx的rewrite规则把index.php去掉。

Thinkphp 框架如果有数据分页的页面需要修改入口文件

//nginx环境下防止U方法输出错误
define('__APP__', '');  //在入口文件index.php中添加这一句

 

第一步:要编辑子站的配置文件

vi  /usr/local/nginx/conf/vhost/子站.conf

把 include enable-php.conf   注释掉这行!(或者直接dd删除)

然后在下面新曾一行  添加 

include enable-php-pathinfo.conf;   #注意后面必须带分号

 

第二步:

cd /usr/local/nginx/conf/vhost/子站.conf 

vi 子站.conf           在你的虚拟主机配置文件server段里添加以下内容:

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

重启nginx!!

更多内容请关注微信订阅号:phpstory

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include servers/*; } 修改什么地方可以来代理http://127.0.0.1:5099
最新发布
07-11
<think>我们正在配置Nginx反向代理,将请求转发到http://127.0.0.1:5099 根据之前的讨论,用户已经通过Homebrew安装了Nginx,并且我们找到了配置文件的位置(例如/opt/homebrew/etc/nginx/nginx.conf) 现在需要修改配置文件,添加一个server块或location块来实现反向代理。 参考引用[2]和[4]中关于反向代理的配置,我们可以这样设置: 1.nginx.conf的http块中,添加一个server块,监听80端口(或443端口,如果需要HTTPS),然后配置location块进行反向代理。 2. 使用proxy_pass指令将请求转发到目标地址。 基本配置示例: 假设我们想要将所有访问本机80端口的请求都转发到http://127.0.0.1:5099,可以这样配置: ```nginx server { listen 80; server_name localhost; # 这里可以改为你的域名,如果只是本地测试用localhost location / { proxy_pass http://127.0.0.1:5099; # 注意这里没有末尾的斜杠,表示将原始请求的URI附加到代理地址后面 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; } } ``` 注意: - 如果proxy_pass的地址末尾有斜杠(例如http://127.0.0.1:5099/),那么会去除location匹配的路径部分,然后将剩余部分附加到代理地址后面。 - 这里我们没有在proxy_pass后面加斜杠,所以请求的URI会原样传递给后端服务器。 另外,根据引用[3],我们还可以设置`proxy_redirect`,但在这个例子中,我们暂时使用默认值(default)即可,所以没有显式设置。 步骤: 1. 打开Nginx配置文件(根据之前找到的路径,例如/opt/homebrew/etc/nginx/nginx.conf)。 2. 在http块中添加上述server配置。 3. 保存文件。 4. 测试配置是否正确:`nginx -t` 5. 重新加载配置:`nginx -s reload` 如果用户希望只代理特定的路径,比如所有以`/api`开头的请求,那么可以修改location的匹配规则: ```nginx location /api/ { proxy_pass http://127.0.0.1:5099/; # 注意这里有斜杠,表示将/api/后面的部分附加到代理地址 # ... 其他proxy_set_header设置 } ``` 这样,当请求`/api/users`时,会被代理到`http://127.0.0.1:5099/users`。 但根据用户需求,他想要代理到http://127.0.0.1:5099,并没有提到特定路径,所以我们使用根路径`location /`。 如果用户有域名,需要将`server_name`改为实际的域名。 注意:如果配置了多个server块,请确保监听端口和server_name不冲突。 最后,请提醒用户修改后测试和重载Nginx。</think>### 配置 Nginx 反向代理到 `http://127.0.0.1:5099` 以下是完整的配置步骤,基于您通过 Homebrew 安装的 Nginx: #### 🔧 配置步骤 1. **打开 Nginx 主配置文件** 根据芯片类型选择路径(Apple Silicon 或 Intel): ```bash # Apple Silicon open /opt/homebrew/etc/nginx/nginx.conf # Intel open /usr/local/etc/nginx/nginx.conf ``` 2. **添加反向代理配置** 在 `http {}` 块内添加以下内容: ```nginx server { listen 80; server_name your-domain.com; # 替换为实际域名或 localhost location / { proxy_pass http://127.0.0.1:5099; # 核心代理指令 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; proxy_redirect off; # 关闭响应头重定向修改[^3] } } ``` #### 📝 关键配置说明 | 指令 | 作用 | 必要性 | |------|------|--------| | `proxy_pass` | 定义后端服务器地址 | ✅ 必须 | | `proxy_set_header` | 传递客户端原始信息 | ✅ 推荐 | | `proxy_redirect off` | 防止后端重定向失效 | ✅ 推荐[^3] | | `listen 80` | 监听 HTTP 端口 | ✅ 必须 | | `server_name` | 服务域名标识 | ✅ 必须 | #### ✅ 验证与应用配置 1. **测试语法**: ```bash nginx -t ``` 成功输出:`configuration file syntax is ok` 2. **重载配置**: ```bash nginx -s reload ``` #### ⚙️ 高级场景配置 **场景1:路径重写** 若需移除 URI 前缀(如 `/api`): ```nginx location /api/ { rewrite ^/api/(.*)$ /$1 break; # 移除 /api 前缀[^1] proxy_pass http://127.0.0.1:5099; } ``` **场景2:静态资源分离** 代理动态请求,直接服务静态文件: ```nginx location / { proxy_pass http://127.0.0.1:5099; } location /static/ { alias /path/to/static/files; # 直接服务静态资源 expires 30d; # 缓存设置 } ``` #### 💡 调试技巧 - 查看访问日志: ```bash tail -f /opt/homebrew/var/log/nginx/access.log ``` - 检查错误日志: ```bash tail -f /opt/homebrew/var/log/nginx/error.log ``` > **注意**:若使用 HTTPS,需额外配置 SSL 证书和 `listen 443` 端口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值