【发布时间】:2017-03-11 14:04:48
【问题描述】:
我发现很多人和我有同样的问题,但我找不到合适的解决方案。
我正在通过 vagrant 和 homestead 运行 NGINX 服务器。在生产端,我使用的是 apache,因此我有一个 htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(^cms-system/public|^assets)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ cms-system/public/$1 [L]
我使用这个 htaccess 将所有 url(除了 cms-system/public 和 assets)重写到我在 cms-system/public 中的 index.php。
我尝试使用此工具将此 htaccess 转换为 nginx 配置:https://winginx.com/en/htaccess 效果不佳。我做了一些调整。异常规则有效,但是我无法让 index.php 的重写工作。有人可以帮我吗?
server {
listen 80;
listen 443 ssl http2;
server_name company.dev;
root "/home/vagrant/company/";
index index.html index.htm index.php;
charset utf-8;
location ~ ^/cms-system/public/(.*) {
}
location ~ ^/assets/(.*) {
}
location / {
if ($request_uri !~ "-f"){
rewrite ^(.*)$ /cms-system/public/$1 break;
}
}
# original location rule
# location / {
# try_files $uri $uri/ /index.php?$query_string;
# }
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/company.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/company.dev.crt;
ssl_certificate_key /etc/nginx/ssl/company.dev.key;
}
【问题讨论】:
-
您确定 PHP7 正在创建
/var/run/php/php7.0-fpm.sock文件吗?默认的 PHP-FPM 池必须使用该套接字文件。 -
嗨。让我帮助你,因为答案真的很可怕。我对以下陈述是否正确? 1) 如果我们请求
/images/logo.png和<root>/images/logo.png未找到,但<root>/cms-system/public/images/logo.png存在 - 需要发送此文件。 2) 通过<root>/cms-system/public/index.php提供的所有未找到请求(/assets/* 除外)。另外,4) 您是否需要像/cms-system/public/index.php或/cms-system/public/images/logo.png这样的直接请求的支持? 5)你需要像/some.php/custom/path/这样的请求吗?