I have been trying this more than 12 hours and still not able to figure the perfect nginx config to route to a /blog wordpress installation. Need this in real urgent basis, have to shift company's blog to new location. Please Help!
I am documenting whatever I have tried so far but still no luck. Please tell me a uniform solution. Have read many blogs, may stackoverflow answers but all are giving different solutions hence no way going forward.
# nginx.conf
upstream wordpress_backend {
server localhost:8002;
}
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
index index.php index.html index.htm;
location / {
#root /usr/share/nginx/html;
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location = /blog {
return 302 /blog/;
}
location /blog/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
#rewrite ^/blog(.*) $1 break;
#proxy_pass http://wordpress_backend/;
proxy_pass http://wordpress:80/;
}
rewrite ^/wp-admin(.*) $scheme://$http_host/blog/wp-admin$1 permanent;
rewrite ^/wp-content(.*) $scheme://$http_host/blog/wp-content$1 permanent;
rewrite ^/wp-includes(.*) $scheme://$http_host/blog/wp-inlcudes$1 permanent;
rewrite ^/wp-login(.*) $scheme://$http_host/blog/wp-login$1 permanent;
# location /blog {
# #root /var/www/html/blog;
# #index index.php index.html index.htm;
# #rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# #try_files $uri $uri/ @blog;
# rewrite /blog(.*) $1 break;
# try_files $uri $uri/ /index.php?$args;
# }
#location @blog {
# rewrite ^/blog/(.*) /$1;
#}
#rewrite /blog/wp-admin$ $scheme://$host$uri/ permanent;
# rewrite /wp-content$ $scheme://$host$uri/blog/wp-content$ permanent;
# location ~ [^/]\.php(/|$) {
# fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# if (!-f $document_root$fastcgi_script_name) {
# return 404;
# }
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_param PATH_INFO $fastcgi_path_info;
# fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
# fastcgi_pass wordpress:9000;
# fastcgi_index index.php;
# }
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
}
As you can see, I have tried a lot of things, I have tried with wordpress fpm and wordpress apache, for fmp I used the /.php location block, for apache I have used proxy_pass, but none of them are giving me, there is either some malformed redirect, sometimes the port is going away, and sometimes the proxy_pass not able to send the correct request to the wordpress.
My docker-compose file
version: '3.1'
services:
wordpress:
image: wordpress:4.7.5-php7.1-apache # have also tried with wordpress:4.7.5-fpm
networks:
- wordpress-test
volumes:
- ./wordpress-data:/var/www/html
#ports:
# - 5002:80
environment:
WORDPRESS_DB_PASSWORD: ithakawordpresspass
depends_on:
- mysql
mysql:
image: mysql:5.7
networks:
- ithaka-test
volumes:
- ./wordpress-db:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: ithakawordpresspass
nginx:
build: nginx
image: aroyd/nginx:latest
networks:
- ithaka-test
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
#- ./nginx:/etc/nginx/conf.d
- ./logs/nginx:/var/log/nginx
#- ./wordpress-data:/var/www/html
ports:
- "5001:80"
depends_on:
- wordpress
#environment:
# - NGINX_HOST=myhost
# - NGINX_PORT=80
networks:
wordpress-test:
external: true
My nginx Dockerfile:
FROM nginx:1.13.0
RUN mkdir -p /var/www/html
RUN chown -R root:root /var/www/html
My wordpress-test network
sudo docker network create --attachable --driver=bridge wordpress-test
So when I am just running wordpress without using nginx, just the apache version then I am able to connect to it directly. But nginx routing giving me all kinds of trouble. Also, when I am using it at location / then its working fine, but my requirement is for location /blog and this is not happening at any cost.
Please tell me how to solve this, either for fpm version or for the apache version, later on I am planning to route the docker nginx which has an exposed port from a standalone nginx from a different host.
Need Help on this urgently. I have looked for many sources. Still looking. Tried many combinations but not being able to solve it.
@tianon @md5 @yosifkit Any update on this? This is like really needed.
Your backend is another container, so it isn't localhost. You need to use the name of the other container, which is wordpress.
@md5 Have tried that too. You can see I have commented the thing in my docker-compose.yml file. I have tried with both.
I meant in your Nginx config.
You also have the Nginx and WordPress containers on two different networks, which won't work.
A bit late and not sure if this is what you need, but this worked for me.
Hey @anirbanroydas, have you found a solution?
I am having a very similar problem. Tried this config first:
~nginx
location /blog {
include '/etc/nginx/snippets/proxy.conf';
proxy_pass http://wordpress:80;
}
~
... which successfully proxies the request to the wordpress container, but does not strip "/blog" from the url. Additionally, wordpress returns a "301 https://mydomain.com:8080" which is very weird because the default port is 80, why redirect to 8080?
Then I've tried this,:
~nginx
location ~ ^/blog(/?.*)$ {
include '/etc/nginx/snippets/proxy.conf';
proxy_pass http://wordpress:80/$1;
}
~
... which fails at nginx level logging this:
~
nginx_1 | 2017/10/02 18:37:26 [error] 55#55: *1341 wordpress could not be resolved (3: Host not found), client: 198.41.230.99, server: mycompany.com, request: "GET /blog/ HTTP/1.1", host: "mycompany.com", referrer: "https://mycompany.com/blog/"
nginx_1 | 198.41.230.99 - - [02/Oct/2017:18:37:26 +0000] "GET /blog/ HTTP/1.1" 502 575 "https://mycompany.com/blog/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36"
~
I'm puzzled about why the second config raises a resolve error while the first does not.
Closing since this isn't really an issue of the image, but rather of how it's configured for access AFAIU (which is out of scope here).
In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow.
I think the content in https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory is probably relevant as well.
Also, with how the image currently works, I was able to trivially get it routing to a subdirectory by using -w on the container run (since it assumes WordPress should be installed in the current working directory):
$ docker run -it --rm -w /var/www/html/wordpress --name some-wordpress wordpress
WordPress not found in /var/www/html/wordpress - copying now...
Complete! WordPress has been successfully copied to /var/www/html/wordpress
...
(which is then accessible via /wordpress when connecting)
Most helpful comment
Also, with how the image currently works, I was able to trivially get it routing to a subdirectory by using
-won the container run (since it assumes WordPress should be installed in the current working directory):(which is then accessible via
/wordpresswhen connecting)