I have a WordPress multisite setup with subdomains on Nginx.
I'd like upload urls such as this to work...
http://mysub.mydomain.com/files/2017/07/myfile.png
where the location of the file is...
<web root>/wp-content/uploads/sites/xx/2017/07/myfile.png
This is the location rule in the Nginx config:
location ~ ^/files/(.*)$ {
try_files /wp-content/uploads/sites/$blogid/$1;
access_log off; log_not_found off; expires max;
}
It doesn't work and I'm unable to see why.
I have also tried (and numerous variations):
location ^~ /files {
internal;
alias /www/wp-content/uploads/sites/$blogid;
access_log off; log_not_found off; expires max;
}
If either of these can be confirmed as 'should work' then I'll start looking at rules higher up my code that may be taking precedence (although nothing's jumped out at me yet)...
This is what did work:
if (!-e $request_filename) {
rewrite ^/files/(.*) /wp-content/uploads/sites/$blogid/$1 last;
}
Related
I am creating an NGINX global config file to be added to a website vhost file.
The code I put in the file /etc/nginx/global.d/wordpresscache.conf is:
set $cache_uri $request_uri;
# POST requests and URLs with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
}
if ($query_string != "") {
set $cache_uri 'null cache';
}
# Don't cache URIs containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php
|wp-.*.php|/feed/|index.php|wp-comments-popup.php
|wp-links-opml.php|wp-locations.php |sitemap(_index)?.xml
|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $cache_uri 'null cache';
}
# Don't use the cache for logged-in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+
|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}
# Use cached or actual file if it exists, otherwise pass request to WordPress
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html
$uri $uri/ /index.php;
}
# Cache static files for as long as possible
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
The problem is that when I reload the configuration I get this error message:
nginx: [emerg] invalid number of arguments in "location" directive in
/etc/nginx/global.d/wordpresscache.conf:35
However the structure of that location directive seem to be good to me. Here is where I got the code from (Tip 7)
Can anyone point me out to what I am doing wrong?
Edit:
I have tried the following:
location ~*. (ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
location ~*. (ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
location ~*\. (ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
But they all give me the same error :(
New Edit. I tried to remove some extensions... and it works. Now I have for testing purposes only:
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf)$ {
expires max;
log_not_found off;
access_log off;
}
and it works. Does anybody know what is the maximum number of arguments in parentheses?
location ~*.(ogg
Missed a space right there, should be:
location ~*. (ogg
The regex in question is a bit off also, because it would match URI /frogg or /blahwav, etc.
The correct is escaping the dot with \:
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
Same thing applies to
location ~ .php$ {
Should be:
location ~ \.php$ {
All in all, that linked article is not escaping much of regexes, for whatever reason.
I suspect it's due to bad formatting/content sanitizing in whatever custom CMS they use.
Example:
location ~* wp-config.php {
deny all;
}
Would unnecessarily match /wp-configaphp or /wp-configuphp, and even /whateveryouwantmetobewp-configaphp, etc. There is no performance benefit in not escaping stuff in those regex, so yeah - "bad blogging" :-)
Should be:
location ~* /wp-config\.php$ {
deny all;
}
And even better, if you know you're using a single site in webroot directory, use exact matching:
location = /wp-config.php {
deny all;
}
You can also look at secure NGINX WordPress configuration which deals with security-related part of NGINX-Wordpress configuration.
Is it possible to enable wordpress on /blog of an existing flask application using nginx? Here is the config i've been working with but not getting anywhere with it. I can get either flask or wordpress to work through nginx but
1) not both at the same time
2) not wordpress with /blog option enabled (wordpress works at / but not /blog)
server {
listen 80;
server_name 0.0.0.0;
#### if I enable the flask app, the blog doesn't work, so how can I keep this as well as add /blog ####
# location / {
# include uwsgi_params;
# uwsgi_pass unix:/var/www/html/cr_webapp/my_app.sock;
#}
#### if I change / to /blog, it starts looking in /usr/share/nginx/html location ####
location = / {
#root /var/www/html/blog;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri?$args;
}
location = /favicon.ico {
root /var/www/html/blog;
log_not_found off;
access_log off;
}
location = /robots.txt {
root /var/www/html/blog;
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ {
root /var/www/html/blog;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
root /var/www/html/blog;
expires max;
log_not_found off;
}
}
I've looked at a link but the solution didn't work for me. Also, when I use /blog, it defaults to a --prefix location, so not sure how I can change that - a link
Is what I am trying to do possible? or i've been ignorant this whole time.
To run WordPress under the /blog prefix, and assuming that it's installed in a directory with the same name, you set root to the directory above.
Use the ^~ modifier, and nested location blocks for anything that belongs to WordPress. See this document for details.
location ^~ /blog {
root /var/www/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri?$args;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}
Assuming that your existing flask configuration works, these lines should be fine:
location / {
include uwsgi_params;
uwsgi_pass unix:/var/www/html/cr_webapp/my_app.sock;
}
The following lines are unrelated to WordPress or flask. If the files exist, set a value for root which points to where the files are located.
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
log_not_found off;
access_log off;
}
I have Ubuntu, nginx, docker.
After running worpdress docker container everything worked fine.
My wordpress site is runing by docker and has address IP:8050
After adding domain name by used proxy_pass in my nginx config i take ERR_ABORTED 404 (Not Found) in statis file (png, css, js)
This is my nginx config:
server {
listen 80;
listen [::]:80;
server_name domain.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
proxy_pass http://IP:8050/;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location ~ ^/css/*(/|$) {
access_log off;
expires max;
}
}
Then I changed url in wp_option in mysql database. Redirect is worked but all statics file is not found. I take error list in my console browser
Example:
GET http://domain/wp-content/plugins/wp-smart-preloader/assets/css/wsp-front-preloader.css?ver=1.11.3 net::ERR_ABORTED 404 (Not Found)
When I comment proxy_pass in nginx config and link to the IP:8050, everything works fine.
Ok, I added only location root [path to worpdress container files]
I have been using the wordpress, shiny-server and RStudio-server on Nginx + Ubuntu 14.04.
Wordpress was installed on a root.
Shiny-server was installed per default procedures.
I added the following codes to /etc/nginx/sites-available/my_site_addresscom.conf to run shiny-server on my_site_address.com/shiny/.
location /shiny/ {
proxy_pass http://my_site_address.com:3838/;
}
At this point, wordpress and shiny-server worked fine.
After that, I decided to add the new wordpress in subdirectory, following the procedures on this site.
Installed Nginx helper plugin, and added the following codes to /etc/nginx/sites-available/my_site_addresscom.conf.
map $http_host $blogid {
default 0;
include /var/www/html/wp-content/uploads/nginx-helper/map.conf;
}
and
location ~ ^/files/(.*)$ {
try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
access_log off; log_not_found off; expires max;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
location ^~ /blogs.dir {
internal;
alias /var/www/html/wp-content/blogs.dir ;
access_log off; log_not_found off; expires max;
}
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
After that, I cannot access to shiny-server with
my_site_address.com/shiny/ while I can access with my_site_address.com:3838/.
Could you please advise me what is wrong?
Thanks in advance!
Self-solved.
It seems that
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { ...
section conflicts with
location /shiny/ { ...
section.
The problem has been solved with modifying '/shiny/' section as follows:
location ^~ /shiny/ { proxy_pass my_site_address.com:3838; }
Thanks and hope this helps somebody.
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;
}