【问题标题】:nginx try_files with $host带有 $host 的 nginx try_files
【发布时间】:2014-02-05 11:37:52
【问题描述】:

在我的配置中,我使用主机名($host)作为目录,所以我不需要为配置中的每个域创建一个条目(仅用于开发目的)

一个问题是 try_files 失败了我尝试了不同的方法,但都没有成功。

  • try_files html\$host$uri =404;
  • try_files $document_root\$host$uri =404;
  • try_files $uri =404;
  • try_files $document_root\$host$fastcgi_script_name =404;

    server {
    listen       80;
    server_name  default;
    
    charset UTF-8;
    
    #access_log  logs/host.access.log  main;
    
    # redirect server error pages to the static pages
    error_page  500 502 503 504  /error/50x.html;
    error_page  404 = /error/404.html;
    error_page  403 = /error/403.html;
    location ^~ /error/ {
        internal;
        root   html/default;
    }
    
    location / {
        root   html/$host;
        index  index.php index.html index.htm;
    }
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        #try_files html\$host$uri =404;
        #try_files $document_root\$host$uri =404;
        #try_files $uri =404;
        #try_files $document_root\$host$fastcgi_script_name =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root\$host$fastcgi_script_name;
        include        fastcgi_params;
    }
    

【问题讨论】:

    标签: web-services nginx fastcgi


    【解决方案1】:

    它本身的概念是正确的,但是你在错误的地方应用它,你应该把它作为服务器的root,而不是try_files

    另外我相信你应该使用$http_host而不是$host,参考this answer了解区别

    不知道为什么你的路径不是绝对的,应该以/开头

    我将对您当前发布的配置添加更改

    server {
        #since you will match multiple domains I removed the server_name
        listen 80 default_server; 
        charset UTF-8; # don't really need this but it won't hurt.
        root   /usr/share/nginx/html/$http_host; #assuming this path
        index  index.php index.html index.htm;
        # redirect server error pages to the static pages
        error_page  500 502 503 504  /error/50x.html;
        error_page  404 = /error/404.html;
        error_page  403 = /error/403.html;
    
        location ^~ /error/ {
            internal;
            root   /usr/share/nginx/html/default;
        }
    
        location / {
            try_files $uri $uri/ /index.php$is_args$query_string;
        }
    
        location ~ \.php$ {
        # these are the minimal required parameters.
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        }
    }
    

    【讨论】:

    • 它是在 php 块中定义的,因此如果 php 文件不存在,我可以捕获“未指定输入文件。”。
    • 你应该在location / 中处理这个问题,让我编辑我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2019-02-09
    • 2023-03-08
    • 2013-12-23
    相关资源
    最近更新 更多