nginx로 모든 요청을 index.php에 다시 작성하십시오.
내 아파치 구성에서 다음과 같은 간단한 재 작성 규칙이 있습니다.
- 파일이 존재하지 않으면 index.php에 다시 작성됩니다.
- URL에서 파일 확장자 (.php)를 볼 수 없습니다.
nginx에서 어떻게 다시 작성할 수 있습니까?
#
# Redirect all to index.php
#
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/[^.]*|\.)$ [NC]
RewriteRule .* index.php [L]
다음은 내 nginx 서버 블록이 지금 어떻게 생겼는지 보여 주지만 작동하지 않습니다.
root /home/user/www;
index index.php;
# Make site accessible from http://localhost/
server_name some-domain.dev;
###############################################################
# exclude /favicon.ico from logs
location = /favicon.ico {
log_not_found off;
access_log off;
}
##############################################################
# Disable logging for robots.txt
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
##############################################################
# Deny all attempts to access hidden files such as
# .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
##############################################################
#
location / {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php$args;
fastcgi_pass 127.0.0.1:9000;
}
###############################################################
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires 30d;
}
###############################################################
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
1 파일이 존재하지 않으면 index.php에 다시 작성됩니다.
다음을 귀하의 location ~ \.php$
try_files = $uri @missing;
이것은 먼저 파일을 제공하려고 시도하고 찾을 수 없으면 @missing
부품으로 이동합니다 . 따라서 다음을 구성 ( location
블록 외부)에 추가하면 인덱스 페이지로 리디렉션됩니다.
location @missing {
rewrite ^ $scheme://$host/index.php permanent;
}
2 URL에 파일 확장자 (.php)가 표시되지 않습니다.
PHP 확장을 제거하려면 다음을 읽으십시오. http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/
및 링크의 예제 구성 :
location / {
set $page_to_view "/index.php";
try_files $uri $uri/ @rewrites;
root /var/www/site;
index index.php index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site$page_to_view;
}
# rewrites
location @rewrites {
if ($uri ~* ^/([a-z]+)$) {
set $page_to_view "/$1.php";
rewrite ^/([a-z]+)$ /$1.php last;
}
}
Perfect solution I have tried it and succeed to get my index page when I have append this code in my site configuration file.
location / {
try_files $uri $uri/ /index.php;
}
In configuration file itself explained that at "First attempt to serve request as file, then as directory, then fall back to index.html in my case it is index.php as I am providing page through php code.
To pass get variables as well use $args
:
location / {
try_files $uri $uri/ /index.php?$args;
}
Flat and simple config without rewrite, can work in some cases:
location / {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/webuser/site/index.php;
}
Using nginx $is_args instead of ? For GET query Strings
location / { try_files $uri $uri/ /index.php$is_args$args; }
Here is what worked for me to solve part 1 of this question:
location / {
rewrite ^([^.]*[^/])$ $1/ permanent;
try_files $uri $uri/ /index.php =404;
include fastcgi_params;
fastcgi_pass php5-fpm-sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
rewrite ^([^.]*[^/])$ $1/ permanent; rewrites non-file addresses (addresses without file extensions) to have a "/" at the end. I did this because I was running into "Access denied." message when I tried to access the folder without it.
try_files $uri $uri/ /index.php =404; is borrowed from SanjuD's answer, but with an extra 404 reroute if the location still isn't found.
fastcgi_index index.php; was the final piece of the puzzle that I was missing. The folder didn't reroute to the index.php without this line.
Here's the answer of your 2nd question :
location / {
rewrite ^/(.*)$ /$1.php last;
}
it's work for me (based my experience), means that all of your blabla.php will rewrite into blabla
like http://yourwebsite.com/index.php to http://yourwebsite.com/index
If you want to pass just the index.php ( no other php file will be passed to fastcgi ) to fastcgi in case you have routes like this in a framework like codeigniter
$route["/download.php"] = "controller/method";
location ~ index\.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
ReferenceURL : https://stackoverflow.com/questions/12924896/rewrite-all-requests-to-index-php-with-nginx
'programing' 카테고리의 다른 글
Django 관리자 이미지 필드에서 이미지 표시 (0) | 2021.01.16 |
---|---|
화면 너비와 높이를 얻는 방법 (0) | 2021.01.16 |
방법 내 방법 (0) | 2021.01.16 |
Ruby 배열의 홀수 (또는 짝수) 항목 (0) | 2021.01.16 |
Android에서 라디오 버튼을 설정하는 방법 (0) | 2021.01.16 |