У меня установлен wordpress в корневом каталоге домена.
Я использую красивые ссылки и быстрые cgi кэширование.
Теперь у меня есть каталог внутри корневого каталога (например, "/ course /"), который содержит некоторые файлы .php, не имеющие ничего общего с wordpress. Я хотел бы получить доступ к этим файлам без расширения .php.
На другом сервере, где все настроено внутри корневого каталога, и я не использую wordpress, он работает с этими дополнениями к серверному блоку:
location / {
try_files $uri $uri.html $uri/ @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Я изо всех сил пытаюсь чтобы он работал на моем сервере Wordpress. Может я что-то упускаю, вот полный блок сервера:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php index.html index.htm;
client_max_body_size 10M;
server_name example.com www.example.com;
location ~* "^/something" {
rewrite ^ http://example.com/curso-gratis-habitos/$ $1?ref=conlasalud permanent;
}
location ~* "^/some-other-thing" {
rewrite ^ https://someotherurl.com/? permanent;
}
location ~* "^/something-else" {
rewrite ^ https://someotherurl.com/? permanent;
}
location ~* "^/another-one" {
return 301 https://someotherurl.com/;
}
location ~* "^/special-url" {
return 301 https://someotherurl.com/;
}
set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
}
if ($query_string != "") {
set $cache_uri 'null cache';
}
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-$
set $cache_uri 'null cache';
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}
location ~ \.php$ {
try_files $uri @extensionless-php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
Перемещение локации /
в блок локация ~ \ .php $
и добавление этих дополнений под ними наконец-то сработало для меня:
location ~ ^/course/(.+)$ {
try_files $uri $uri/ @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Здесь это полный блок сервера для справки:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/example.com/html;
index index.php index.html index.htm;
client_max_body_size 10M;
server_name example.com;
location ~* "^/something" {
rewrite ^ http://example.com/curso-gratis-habitos/$ $1?ref=conlasalud permanent;
}
location ~* "^/some-other-thing" {
rewrite ^ https://someotherurl.com/? permanent;
}
location ~* "^/something-else" {
rewrite ^ https://someotherurl.com/? permanent;
}
location ~* "^/another-one" {
return 301 https://someotherurl.com/;
}
location ~* "^/special-url" {
return 301 https://someotherurl.com/;
}
set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
}
if ($query_string != "") {
set $cache_uri 'null cache';
}
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $cache_uri 'null cache';
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php;
}
location ~ ^/course/(.+)$ {
try_files $uri $uri/ @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# Enable the hide backend feature - Security > Settings > Hide Login Area > Hide Backend
rewrite ^(/)?somesecreturl/?$ /wp-login.php?$query_string break;
# Protect System Files - Security > Settings > System Tweaks > System Files
location ~ /\.ht { deny all; }
location ~ wp-config.php { deny all; }
location ~ readme.html { deny all; }
location ~ readme.txt { deny all; }
location ~ /install.php { deny all; }
location ^wp-includes/(.*).php { deny all; }
location ^/wp-admin/includes(.*)$ { deny all; }
# Disable PHP in Uploads - Security > Settings > System Tweaks > Uploads
location ^wp\-content/uploads/(.*).php(.?) { deny all; }
}
Скорее всего, проблема здесь в том, что у вас есть блок расположения для .php$ два раза, а второй из этих блоков никогда не достигается, так как первый совпадает со всеми PHP-файлами.
Более простым решением было бы следующее:
location ~ ^/course/(.+)$ |
try_files $1.php $1;
}
и не иметь местоположения @extensionless-php
, второй местоположение ~ \. php$
блоки в конфигурации.
Этот блок расположения заставляет nginx сначала искать сам URI для URI, начиная с /course/
, и если он не найден, попробуйте найти расширение URI + .php
. Имя файла с расширением PHP будет передано в обычный блок обработки PHP. Этот блок должен быть выше строки ~ \.php$ {
, так как в противном случае этот блок всегда используется первым.