Apache Nginx配置/重写
我想将nginx配置为以下方式: [确定]如果我浏览到domain.com/,将调用/var/www/index.php文件 [HOW?]如果我浏览domain.com/blah,就会调用/var/www/controller.php 在apache上,通过重写条件完成:Apache Nginx配置/重写,apache,.htaccess,nginx,rewrite,config,Apache,.htaccess,Nginx,Rewrite,Config,我想将nginx配置为以下方式: [确定]如果我浏览到domain.com/,将调用/var/www/index.php文件 [HOW?]如果我浏览domain.com/blah,就会调用/var/www/controller.php 在apache上,通过重写条件完成: RewriteRule (.*) controller.php [L,QSA] RewriteRule ^api api/controller.php [L,NC] RewriteRule \.(js|css|gif|pn
RewriteRule (.*) controller.php [L,QSA]
RewriteRule ^api api/controller.php [L,NC]
RewriteRule \.(js|css|gif|png|jpg|ico|txt|woff|woff2)$ - [L,NC]
[如何?]如果我浏览domain.com/api/someMethod,就会调用/var/www/api/controller.php
在apache上,通过重写条件完成:
RewriteRule (.*) controller.php [L,QSA]
RewriteRule ^api api/controller.php [L,NC]
RewriteRule \.(js|css|gif|png|jpg|ico|txt|woff|woff2)$ - [L,NC]
[如何?]如果我浏览domain.com/image.png,nginx将显示image/var/www/image.png
在apache上,通过重写条件完成:
RewriteRule (.*) controller.php [L,QSA]
RewriteRule ^api api/controller.php [L,NC]
RewriteRule \.(js|css|gif|png|jpg|ico|txt|woff|woff2)$ - [L,NC]
和我的nginx配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
listen [::]:443 ssl;
server_name _;
root /var/www;
index index.php;
ssl_certificate /etc/ssl/certs/server.crt;
ssl_certificate_key /etc/ssl/private/server.key;
location ~ {
try_files $uri $uri/ /index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
配置的重要部分包括:
root /var/www;
index index.php;
location = / { }
location / {
try_files $uri $uri/ /controller.php;
}
location /api {
try_files $uri $uri/ /api/controller.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
前两行来自原始配置
location=
将确保URI/
遵守索引index.php
而不是下一步的try\u files
规则
location/
定义默认操作,在子目录上提供image.png
,index.php
,在其他任何目录上提供controller.php
location/api
针对controller.php
修改此行为
location~\.php$
块实现了fastcgi
接口。请注意,包括fastcgi_参数
应位于任何fastcgi_param
指令之前,以避免后者被静默重写
如果希望URIdomain.com/blah.php
调用controller.php
而不是抛出404,那么将=404
更改为/controller.php
有关nginx
指令的列表,请参阅