Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

nginx try_files with alias doesn't work, but rewrite does

I have a few websites that load everything from /static/ folder, exception being the /blog/ subfolder.

With the following nginx configuration, it works because of the @wp named location (doing the rewrite).

I can't make it work without rewrite. With try_files $uri $uri/ /blog/index.php$is_args$args =404; it doesn't enter the /blog/.*php location. Need to do it because it's not always "blog" in the path.

index index.html index.htm index.php static.php;
add_header X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";
# $domain is a mapped variable to make it work on staging/production servers.
root /home/static/$domain;

try_files $uri $uri/ =404;


location /static {
    # all redirect to /static/index.php if files/directory not found
        try_files $uri $uri/ /index.php$args;
}

location @wp {
  #rewrite ^/blog(.*) /index.php?q=$1;
    rewrite ^ /blog/index.php last;
}

# location ~ /(blog|stuff) { # this is not working

location ^~ /blog {
    alias /home/wordpress/$domain;
    -------- section to consider ----------
    #this does not work
    #try_files $uri $uri/ /blog/index.php$is_args$args =404;

    #any of the lines below works
    #if (!-e $request_filename) { rewrite ^ /blog/index.php last; }
    try_files $uri $uri/ @wp;
    --------- end of section to consider -----
    # location for any php under /blog
    location ~ /blog/.+.php$ {
        # any of the following 2 lines work
        #if (!-f $request_filename) { rewrite ^ /blog/index.php last; }
        try_files $uri $uri/ @wp;
      
        include /etc/nginx/fastcgi_params;
        # we split the path to return the php script (remove /blog from the beginning)
        fastcgi_split_path_info       ^/blog(.+.php)(.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_index  index.php;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        }
}


location ~ .php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}


location ~ .(jpg|jpeg|gif|png|css|js|ico|svg|eot|ttf|woff|woff2|otf)$ {
        access_log        off;
        expires           30d;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...