2016-02-05 66 views
1

我新來nginx如何使用htacces在nginx的子文件夾中運行index.php?

我堆棧運行我的網站使用nginx

我已嘗試將htaccess轉換爲使用nginx.confdefault.d/*.conf但我的網站仍然無法正常工作。

這裏是我的文件:

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

第一htaccess的

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

第二htaccess的同一文件夾index.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的運行呢?

回答

0

一種選擇是設置.../public作爲文檔根目錄,並重寫任何URI年初/public//

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; 
    ... 
} 
相關問題