簡體   English   中英

用於網址路由的nginx.conf

[英]nginx.conf for url routing

我正在嘗試使用index.php來處理http路由,因此請確保我的應用程序安靜。

我在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>

您可能想嘗試這樣的方法,對我來說就像是一種魅力:

location / { 
    try_files $uri $uri/ @rules; 
} 

location @rules { 
    rewrite ^/(.*)$ /index.php?param=$1; 
}

這將查找/的位置,這是您的Web根目錄。 在此目錄中找到所有可訪問Web的文件。 如果文件存在,它將帶您到該文件。 如果沒有,它將把您帶入@rules塊。 您可以使用regexp匹配來更改網址格式。 但簡而言之, (.*)匹配您網址中的任何字符串,並將您帶到索引。 我稍微修改了您寫的內容,以將原始輸入作為參數輸入到index.php中。 如果不這樣做,您的腳本將沒有任何有關如何路由請求的信息。

例如,只要/blablabla不是目錄,然后轉到/blablabla將屏蔽該URL,但拉出/index.php?param=blablabla

希望這可以幫助!

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM