[英]nginx.conf for url routing
I've used the try_files directive within nginx.cong but did'nt work, I hit /blablabla and instead of going through index.php it throws a 404. Here's my current nginx.conf 我在nginx.cong中使用了try_files指令,但是没有用,我打了/ blablabla,而不是通过index.php而是抛出了404。这是我当前的nginx.conf
<pre>
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
location / {
try_files $uri $uri/ /index.php;
}
}
}
</pre>
You might want to try something like this, works like a charm for me: 您可能想尝试这样的方法,对我来说就像是一种魅力:
location / {
try_files $uri $uri/ @rules;
}
location @rules {
rewrite ^/(.*)$ /index.php?param=$1;
}
This looks for the location at /
which is your web root. 这将查找/
的位置,这是您的Web根目录。 All of your web accessible files are found in this directory. 在此目录中找到所有可访问Web的文件。 If a file exists, it'll take you to that file. 如果文件存在,它将带您到该文件。 If not, then it'll throw you into the @rules block. 如果没有,它将把您带入@rules块。 You can use regexp matching to vary your url formatting. 您可以使用regexp匹配来更改网址格式。 But in short, the (.*)
matches any string in your url and takes you to your index. 但简而言之, (.*)
匹配您网址中的任何字符串,并将您带到索引。 I modified what you had written slightly to feed the original input in to index.php as a parameter. 我稍微修改了您写的内容,以将原始输入作为参数输入到index.php中。 If you don't do this, your script won't have any info about how to route the request. 如果不这样做,您的脚本将没有任何有关如何路由请求的信息。
For example, then going to /blablabla
will mask the url but pull up /index.php?param=blablabla
so long as /blablabla
isn't a directory. 例如,只要/blablabla
不是目录,然后转到/blablabla
将屏蔽该URL,但拉出/index.php?param=blablabla
。
Hope this helps! 希望这可以帮助!
server {
listen 80;
server_name example.com;
index index.php;
error_log /path/to/example.error.log;
access_log /path/to/example.access.log;
root /path/to/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.