My forum is installed on the url: example.com/forums
I have used nginx and Vanilla to "prettify" the urls. I've set
/forum/conf/config.php, “RewriteUrls” to “True”.
and in my nginx.conf:
location /forums {
index index.php index.htm index.html;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 30d;
}
try_files $uri $uri/ #forums;
}
location #forums {
rewrite ^/forums(.+)$ /forums/index.php?p=$1 last;
}
The problem is I installed the sitemap plugin by Vanilla Forums.
and the resulting sitemap is supposed to be located at
example.com/forums/sitemapindex.xml
But when I navigate there nginx gives me a 404.
How do I solve this?
The problem is that the URI /forums/sitemapindex.xml is being processed by the location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ block and not being forwarded to /forums/index.php.
If you do not serve static .xml files, you could simply remove the |xml term from the regular expression.
Otherwise, you will need to make that URI a special case, for example:
location = /forums/sitemapindex.xml {
rewrite ^ /forums/index.php?p=/sitemapindex.xml last;
}
Related
I have a general nginx rule to serve jpgs from their URI.
So if the URI is "http://example.com/images/1.jpg" it will serve under the form root of the site/images/1.jpg
I want to try and serve the image from alternative path if not found on the original path. How do I write the second location?
Here is what I got:
location ~* ^.+\.(jpg|jpeg|gif|png|svg|js|css|mp3|ogg|mpe?g|avi|zip|gz|bz2?|rar|swf|ico)$ {
try_files $uri $uri/ #fallback;
access_log off;
expires max;
log_not_found off;
}
How do I write the fallback to look for the files in another location like /home/user/anotherfolder?
You can use a named capture in the regular expression location to save the filename for later. You can cascade named locations, to try different roots:
location ~* ^/images(?<filename>/.+\.jpg)$ {
try_files $uri #other;
}
location #other {
root /path/to/alternate;
try_files $filename #fallback;
}
If there is a suitable common parent directory, you can achieve the same thing in a single location block.
location ~* ^/images(?<filename>/.+\.jpg)$ {
root /path/to/ancestor;
try_files /dir/$uri /other/dir/$filename #fallback;
}
I have a nginx server with the following code added to the sites conf file. The first part is an alias to allow the folder called images to be severed when visiting for example: example.com/images
The second part has been added to allow permalinks in wordpress to work. Problem is each of the code blocks work separately but not together. The offending line of code is:
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
This code stops any files being server from example.com/images and shows a 404 error
location /images {
alias /var/www/clients/client0/web6/images;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
Why does the cache line conflict?
Use try_files. This way you can work with settings in another block.
Example url: http://your-site.com/img/lorena_improta.jpg
root /var/www/html/stackoverflow;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~* ^/img/(.*)$ {
try_files $uri /48725060/images/$1;
}
location ~* \.(jpe?g|gif|png) {
expires 1h;
}
How can I exclude all URLs with a directory called dynamic in the following location block:
location ~* \.(?:js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
Here's the entire config, most of this comes from herokus php nginx buildpack
http://pastebin.com/xQ4BDtwr
( stackex won't let me post "mostly code" )
I would add the following location:
location /path/to/dynamic/ {
location ~* \.js$ {internal;}
}
The key is to override the ~* \.(?:js)$ regex location with a prefix location. Then you don't have to worry about where it appears in your config.
It could be solved with another regex location ~ /dynamic/.*\.js$ {internal;}, but then you would need to be sure it always comes before the ~* \.(?:js)$ location; another problem waiting to happen when your config grows.
I have an webapp that uses FastCGI. This app is deployed as different "sites". I have a general nginx configuration for the app, and then, I include upstrams and locations files for each site. Each site needs some .js and .css files. Right now, I have set these via the root of the general configuration file. My requirment now, is that I want to be able to use different .css and .js for different sites. So the lookup of the files, should first start at the site/location level and if not found, then search in the general root.
The general nginx conf file is:
# Change this directory depending on the server
include /etc/nginx/conf.d/app/upstreams/*.conf;
server
{
server_name 24.39.17.76;
root /var/www/app/files/;
listen 443;
ssl on;
ssl_certificate /etc/nginx/conf.d/app/ssl/new/app.pem;
ssl_certificate_key /etc/nginx/conf.d/app/ssl/new/app.com.key;
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|svg|woff|ttf)$ {
access_log off; # this is because otherwise, with jail2ban the user may be banned
# add_header Access-Control-Allow-Origin "*";
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
add_header Pragma public;
}
# Change this directory depending on the server
include /etc/nginx/conf.d/app/locations/*.conf;
}
Note the lines where I include all locations conf and upstreams files.
Then a particular site, has a upstream file like:
upstream siteX
{
server localhost:9021;
server localhost:9022;
server localhost:9023;
}
And a location file like this one:
location ~ ^/siteX/reps {
access_log /var/log/nginx/app/siteX_reps_access.log;
error_log /var/log/nginx/app/siteX_reps_error info;
if ($arg_service = "true") {
rewrite ^/(.*)$ /service/$1 last;
}
rewrite ^/siteX/(.*)$ /$1 break;
try_files $uri #fastcgiSiteX;
}
location #fastcgiSiteX {
include fastcgi_params;
fastcgi_param REQUEST_URI $uri?$args;
fastcgi_pass fastcgiSiteX;
fastcgi_next_upstream error invalid_header http_500;
}
The html of my webpage that includes the css and js is like this:
<script type="text/javascript" src="/JQDevelopmentLibrary/jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="/TBSDevelopmentLibrary/css/bootstrap.css"/>
But I can easily add a prefix if that helps with any possible solution, like:
<script type="text/javascript" src="/siteX/webfiles/JQDevelopmentLibrary/jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="/siteX/webfiles/TBSDevelopmentLibrary/css/bootstrap.css"/>
SO...the general files are under /var/www/app/files/. However, I would like that for siteX, the files are first searched in another path, say /var/www/siteX/files and only if not found, searched in the general one.
One solution I tried but didn't work is to define this try_files expression:
try_files /siteX/webfiles$uri $uri #fastcgiSiteX;
And then, I put all the folders and files in $document_root/siteX/webfiles, that is, /var/www/app/files/siteX/webfiles/. I thought this solution should work, but it didn't.
Any ideas?
Thanks in advance,
its kind of a hack but it can be done.
location ~ ^/siteX/reps {
access_log /var/log/nginx/app/siteX_reps_access.log;
error_log /var/log/nginx/app/siteX_reps_error info;
if ($arg_service = "true") {
rewrite ^/(.*)$ /service/$1 last;
}
rewrite ^/siteX/(.*)$ /$1 break;
try_files $uri #hack;
}
location #hack {
root /var/www/siteX/files;
try_files $uri $uri/ #fastcgiSiteX;
}
location #fastcgiSiteX {
include fastcgi_params;
fastcgi_param REQUEST_URI $uri?$args;
fastcgi_pass fastcgiSiteX;
fastcgi_next_upstream error invalid_header http_500;
}
This is my nginx configuration file:
server {
listen 80;
server_name localhost;
location / {
root d:/www;
index index.html index.htm;
}
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
}
}
And the directory of the document is like this:
D:/workspace/javascript/maplib
-- v1.0
--main.js
-- v1.1
Now I want to access the v1.0/main.js by http://localhost/js/api/v1.0/main.js.
And it returns a 404 error.
It seems that ngnix will tried to get the file through D:/workspace/javascript/maplib/js/api/v1.0/main.js which does not exist.
It seems that the string path in the location(in the url) must exist at the file system.
How to fix it to meet my requirement?
BTW, there is not only the js but also some other kinds of files like .gif,.png,.html inside the D:/workspace/javascript/maplib/.
Use alias. Ref: http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
That is, replace
root D:/workspace/javascript/maplib/;
by
alias D:/workspace/javascript/maplib/;
Use rewrite inside location /js/api/ for this, like:
rewrite ^/js/api(.*)$ $1;
You can use root with try_files, just add the try_files line
location /js/api/ {
root D:/workspace/javascript/maplib/;
autoindex on;
try_files $uri $uri/ =404;
}