gpt4 book ai didi

php - 如何使用htaccess在nginx的子文件夹中运行index.php?

转载 作者:行者123 更新时间:2023-12-04 16:48:04 26 4
gpt4 key购买 nike

我是 nginx 的新手。

我使用 nginx 运行我的网站。

我已经尝试将 htaccess 转换为使用 nginx.confdefault.d/*.conf 但我的网站仍然无法正常工作.

这是我的文件:

--- .htaccess ---
|-- public |
| --- index.php
| --- .htaccess
|-- application

第一个 htaccess

RewriteEngine on
RewriteRule ^(.*) public/$1 [L]

第二个 htaccessindex.php

相同的文件夹
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

这是我的 nginx.conf 编辑后:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
autoindex off;
location / {
# first htaccess configuration
rewrite .* /public/index.php last;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

default.d/*.conf

index index.php index.html index.htm;
# htaccess configuration
autoindex off;
if(!-e $request_filename){
rewrite^(.+)$ /index.php?url=$1 break;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}

谁能帮我在 nginx 中运行这个?

最佳答案

一种选择是将.../public设置为文档根目录,并将任何以/public/开头的URI重写为/ :

root /usr/share/nginx/html/public;

location / {
try_files $uri @index;
}
location @index {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location /public/ {
rewrite ^/public(.*)$ $1 last;
}
location ~* \.php$ {
try_files $uri =404;
...
}

但是,如果您更喜欢使用当前的文档根目录,这可能符合您的要求:

root /usr/share/nginx/html;

location / {
try_files $uri /public$uri @index;
}
location @index {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
location ~* \.php$ {
try_files $uri /public$uri =404;
...
}

关于php - 如何使用htaccess在nginx的子文件夹中运行index.php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35229725/

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