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


How use htacces to run index.php in subfolder in nginx?

我是nginx的新手。

我使用nginx堆叠运行我的网站。

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

这是我的文件:

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

第一个访问

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;
    ...
}