How do I correctly use try_files when looking in two different directories for files to serve?

Rimble

I'm quite new to Nginx so I might be misunderstanding of what try_files can do.

For my local development set up I have multiple installations that will each be accesible via their own subdomain. These installations are being migrated into a new folder structure but I still want to have the ability to support both at the same time. When pulled via git the new full path looks like this :

/home/tom/git/project/v3/[installation]/public/

The old structure goes 1 directory deeper namely as follows:

/home/tom/git/project/v3/[installation]/workspace/public

Where installation is variable according to the installation name and the /public folder will be the root for nginx to work from.

The root is determined by the subdomain and is extracted via regex like so:

server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;

So far I've managed to get all this working for one of the folder structures but not both at the same time. My Nginx configuration for this local domain looks like this. Below is what I've tried but just can't seem to get working. As soon as I pass the @workspace named location as fallback for try_files it always defaults to 404.

index index.html index.htm index.nginx-debian.html index.php;

server_name ~^(?<subdomain>[^.]+)\.local\.project\.test;

root /home/tom/git/project/v3/$subdomain/public/;

location / {
        try_files $uri @workspace =404;
}

location @workspace {
        root /home/tom/git/project/v3/$subdomain/workspace/public/;
        try_files $uri =404;
}

I have also tried shortening the root and passing the following parameters to try_files

root /home/tom/git/project/v3/$subdomain;
location / {
        try_files /public/$uri /workspace/public/$uri =404;
}

But this still defaults to a 404, with a $uri/ as a third parameter there it will emit a 403 forbidden trying to list the directory index of the root.

I hope someone can provide some advice or an alternative as to how to approach this issue I am facing. If I need to provide additional data let me know,

Thanks in advance.

Richard Smith

The named location must be the last element of a try_files statement.

For example:

location / {
    try_files $uri @workspace;
}
location @workspace {
    ...
}

See this document for details.


The $uri variable includes a leading /, so your constructed pathnames contain a // which may be why they fail.

For example:

location / {
    root /home/tom/git/project/v3/$subdomain;
    try_files /public$uri /workspace/public$uri =404;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

index when serving 2 merged directories in nginx via try_files

分類Dev

How do I serve static files only to authorized users?

分類Dev

How do I serve static files only to authorized users?

分類Dev

How do I serve static files only to authorized users?

分類Dev

How to serve static files to different routes in Express?

分類Dev

Bundling files in different directories?

分類Dev

How to compare two directories and delete duplicate files

分類Dev

How can i serve static files in django?

分類Dev

Serve files from a different directory when request comes to root directory

分類Dev

How do I use two different relativeTime customizations?

分類Dev

How can I use bucket storage to serve static files on google flex/app engine environment?

分類Dev

Use two/multiple selected directories from custom page in Files section

分類Dev

How do I list the files having particular strings from group of directories in bash?

分類Dev

How do I copy columns of two files as rows of a third file

分類Dev

How do I merge two *.avi files into one

分類Dev

How do I prevent two OSes from clobbering grub files?

分類Dev

How to diff file names in two directories (without writing to intermediate files)?

分類Dev

How to find / list all unique files across two directories?

分類Dev

How can I serve my static files with Node/express?

分類Dev

How do I use a batch file to recursively replace a string in files?

分類Dev

How can I count files with a particular extension, and the directories they are in?

分類Dev

Can I use "git checkout --" on two files?

分類Dev

When I use scanf(), I wonder what is different in these two

分類Dev

merging particular text files in different directories

分類Dev

In SQL, how do I select unique user id when it may have two different observations?

分類Dev

how can I create two files with same name with different case in mac osx

分類Dev

Git: How to diff two different files in different commits?

分類Dev

How do I correctly use Control.Exception.catch in Haskell?

分類Dev

How do I use GPL and MIT licenses correctly?

Related 関連記事

  1. 1

    index when serving 2 merged directories in nginx via try_files

  2. 2

    How do I serve static files only to authorized users?

  3. 3

    How do I serve static files only to authorized users?

  4. 4

    How do I serve static files only to authorized users?

  5. 5

    How to serve static files to different routes in Express?

  6. 6

    Bundling files in different directories?

  7. 7

    How to compare two directories and delete duplicate files

  8. 8

    How can i serve static files in django?

  9. 9

    Serve files from a different directory when request comes to root directory

  10. 10

    How do I use two different relativeTime customizations?

  11. 11

    How can I use bucket storage to serve static files on google flex/app engine environment?

  12. 12

    Use two/multiple selected directories from custom page in Files section

  13. 13

    How do I list the files having particular strings from group of directories in bash?

  14. 14

    How do I copy columns of two files as rows of a third file

  15. 15

    How do I merge two *.avi files into one

  16. 16

    How do I prevent two OSes from clobbering grub files?

  17. 17

    How to diff file names in two directories (without writing to intermediate files)?

  18. 18

    How to find / list all unique files across two directories?

  19. 19

    How can I serve my static files with Node/express?

  20. 20

    How do I use a batch file to recursively replace a string in files?

  21. 21

    How can I count files with a particular extension, and the directories they are in?

  22. 22

    Can I use "git checkout --" on two files?

  23. 23

    When I use scanf(), I wonder what is different in these two

  24. 24

    merging particular text files in different directories

  25. 25

    In SQL, how do I select unique user id when it may have two different observations?

  26. 26

    how can I create two files with same name with different case in mac osx

  27. 27

    Git: How to diff two different files in different commits?

  28. 28

    How do I correctly use Control.Exception.catch in Haskell?

  29. 29

    How do I use GPL and MIT licenses correctly?

ホットタグ

アーカイブ