gpt4 book ai didi

url-rewriting - 用 nginx 重写对 index.php 的所有请求

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:42:44 25 4
gpt4 key购买 nike

在我的 apache 配置中,我有以下简单的重写规则

  1. 除非文件存在,否则将重写为 index.php
  2. 在 url 上您永远看不到文件扩展名 (.php)

如何在 nginx 中重写它?

#
# Redirect all to index.php
#
RewriteEngine On

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]

这是我的 nginx 服务器 block 现在的样子,但它不起作用 :(

root /home/user/www;
index index.php;

# Make site accessible from http://localhost/
server_name some-domain.dev;


###############################################################
# exclude /favicon.ico from logs
location = /favicon.ico {
log_not_found off;
access_log off;
}

##############################################################
# Disable logging for robots.txt
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

##############################################################
# Deny all attempts to access hidden files such as
# .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}

##############################################################
#
location / {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php$args;
fastcgi_pass 127.0.0.1:9000;
}

###############################################################
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}

###############################################################
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}

最佳答案

我已经尝试过并成功获得了我的索引页。当我在我的站点配置文件中添加此代码时:

location / {
try_files $uri $uri/ /index.php;
}

在配置文件本身中解释了这些是配置的步骤

First attempt to serve request as file,then as directory,then fall back to index.html

在我的例子中,它是 index.php,因为我通过 php 代码提供页面。

关于url-rewriting - 用 nginx 重写对 index.php 的所有请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53470903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com