我正在尝试设置一个别名,以便我可以导航到 ww。*.com/dev/ 并让它从根目录之外提供文件服务。
我尝试过的事情
- 确保 php 块位于位置块中
- 尝试用 root 替换别名
- 尝试注释掉包含 snippets/fastcgi-php.conf;
- 尝试了针对嵌套块内的 try_files 建议的修复方法。
根目录 - /var/www/nginx/www/a ailas - /var/www/nginx/www
我的别名
location /dev {
alias /var/www/nginx/www;
location ~ /([^/]+\.php)$ {
include snippets/fastcgi-php.conf;
# try_files /$1 =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
# fastcgi_index index.php;
include fastcgi_params;
}
}
片段/fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi配置文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
任何帮助将不胜感激
答案1
尝试:
location ^~ /dev {
alias /var/www/nginx/www;
if (!-e $request_filename) { rewrite ^ /dev/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
^~
在块上使用修饰符location
可避免与其他正则表达式位置块发生潜在冲突。请参阅这个文件了解详情。
避免同时使用try_files
和,alias
因为长期存在的问题。
您的文件的大部分snippets/fastcgi-php.conf
内容与 pathinfo 的使用有关,但您似乎并未使用它。
看这种警告关于 的使用if
。