【问题标题】:How use htacces to run index.php in subfolder in nginx?如何使用 htaccess 在 nginx 的子文件夹中运行 index.php?
【发布时间】:2016-05-15 18:13:25
【问题描述】:

我是 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 中运行它?

【问题讨论】:

    标签: php apache .htaccess nginx


    【解决方案1】:

    一种选择是将.../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;
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 1970-01-01
      • 2013-11-14
      • 2019-01-28
      • 2013-04-19
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多