Being building a virtualhost for wordpress, I would like to know if this set seems correct or not at all.
location / {
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
The second line comes from Nginx's website, in an example concerning Wordpress so not knowing if I should keep the first one or not; as much as I ask before doing stupid things. Can it be possible to directly merge the two parts into one ?
You defenetly need only one rule to match "/" because the second rule will never be matched, so your rule should look like this:
location / {
try_files $uri $uri/ /index.php?$args;
} You can find more about nginx locations here: Understanding Nginx Server and Location Block Selection Algorithms
Related
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
I am new to nginx and trying to understand the code above. This is how I am understanding it so far. I would appreciate any explanations if I am missing the point.
location / -> means nginx server will look at any url with that path. For example: www.cookie.com/, www.cookie.com/cake, www.cookie.com/cake/weddings will all hit this location.
root /usr/share/nginx/html; -> means it will look in the root folder where nginx is located in my docker
3.try_files $uri $uri/ /index.html; -> I am really not understanding the $uri $uri/. I know having thos $uris means it's looking for a directory with that html page. Does that mean if I have a URL such as www.cookie.com/cake/wedding/html, the first $uri will stop at cake and the second $uri at wedding and then finally to the index html if there is one there?
4.index index.html index.htm; --> if there is an index file there as html or html it should serve it.
Please let me know if I got this right.
I need to match
http://local.com/#app
http://local.com/app
to the same subdirectory.
Trying to put regex in the location. But it's not working at all.
Any idea?
server {
server_name local;
location ^~ /(.*)app {
alias /var/www/app;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
Anything after # is not processed by web server. So this would not work at the NGINX level. But what you can do is at the backend, where will be serving "/" files, like if you are using NodeJS or Python, you can easily redirect http://local.com/#app to http://local.com/app
I have developers who will be working on their local machines editing multiple Wordpress sites. I'd like to set up Nginx for them one time without the need for them to edit the config file in the future. Typically when Nginx is configured to host Wordpress, a location block such as this is included:
location / {
try_files $uri $uri/ /index.php$is_args$args;
} # End location
In our situation, each WP site will be in its own subdirectory. So when a developer needs to view a site, they'll go to a URL in their browser such as:
http://localhost/site1
http://localhost/site2
http://localhost/site3
What we would like is for the location directive above to include the subdirectories. As it is now, it only includes the root (http://localhost) and not the subs. I think this requires a wildcard or regex of some kind, but I'm not sure.
In other words, I think I'm looking for a location block like:
location /all-subdirectories {
try_files $uri $uri/ /whatever-subdirectory/index.php$is_args$args;
} # End location
Does this make sense or am I on the wrong track?
You could use a regular expression location to capture the first part of the URI, for example:
location ~ ^(/[^/]+) {
try_files $uri $uri/ $1/index.php?$args;
}
Or use a named location with one or more rewrite statements, for example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^(/[^/]+) $1/index.php last;
}
I have a very simple js server (w/ webpack) serving a simple client-side app. My configuration file looks like this:
server {
listen 80;
root /home/user/project/dist;
index index.html;
server_name project.example.com;
location / {
try_files $uri $uri/ =404;
}
}
I have tried many combinations of try_files. I don't want it to 404 when it can't find something other than index.html (which is what it currently does). It works OK for the root (pointing the browser to project.example.com, but anything else gives a 404.
To solve this, I've tried to set try_files in many combinations, such as:
try_files $uri index.html;
try_files $uri $uri/ index.html;
try_files $uri /absoulute/path/to/index.html;
They either 404 everything or give a 500, which, according to nginx logs, is an infinite redirect loopback.
I don't think what I'm doing is terribly complex, but everything I've tried so far hasn't done what I want, which is to allow react-router to take care of everything by having try_files just give everything to index.html.
Any help appreciated.
nginx URIs contain a leading /. If the URI http://project.example.com/index.html works, then the try_files statement should look like:
try_files $uri $uri/ /index.html;
See this document for details.
I use nginx
location / {
try_files $uri /index.html;
}
and it works well. However it doesn't have to look up for the $uri file.
I saw that it is possible to use empty string instead of $uri, which could be faster, i.e.
location / {
try_files '' /index.html;
}
but it just doesn't work for me. So how to do it the fastest way? Or maybe using rewrite would be faster?