Score:0

reverse proxy for subdirectory on another server with wordpress for blog

in flag

I have a website and it's working now as

https://example.com

Now I want to load my blog as a subdirectory but on another server, it should be loaded as

https://example.com/blog

I am using nginx webserver and Cloudflare DNS service, and I know that I should use a reverse proxy for this purpose, but couldn't make it work yet

I need to know, what DNS record I should add and what nginx config I should use

djdomi avatar
za flag
Does this answer your question? [Nginx reverse proxy + URL rewrite](https://serverfault.com/questions/379675/nginx-reverse-proxy-url-rewrite)
MohsenP avatar
in flag
thank you for sending this URL, but I couldn't get it working with what was described there
ws flag
Cloudflare *is* a reverse proxy.
MohsenP avatar
in flag
thank you, I am using Cloudflare DNS service
Score:0
in flag

Finally, after a lot of work on the problem, I found the right answer, I am on Ubuntu 22, nginx 1.25, the main website is based on Django and the blog is based on WordPress, and these websites are on different servers

first, create an A record for the subdomain, which points to the blog server IP address, for example A blog 127.0.0.1

after that, add this to the main website nginx config file

location  /blog/ {
    proxy_pass http://blog.example.com/;
    proxy_read_timeout 90;
    proxy_connect_timeout 90;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host blog.example.com;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header Connection “”;
}

and finally, create a config file for the blog website on the blog server

server {
    server_name www.example.com/blog;
    root /var/www/blog;
    access_log  /var/log/nginx/blog.access.log  main;


    add_header 'Access-Control-Allow-Origin' "https://www.example.com";
    add_header 'Access-Control-Allow-Methods' 'GET, POST';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header Access-Control-Allow-Headers User-Agent,Keep-Alive,Content-Type;
    add_header 'Referrer-Policy' 'origin';


    location /wp-admin/ {
        index index.php;
        try_files $uri $uri/ /index.php$args;
    }


    location / {
        root /var/www/blog;
        index index.php index.html index.htm index.nginx-debian.html;
        try_files $uri $uri /index.php?q=$uri&$args;
    }


    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        include snippets/fastcgi-php.conf;
        fastcgi_read_timeout 120;
        fastcgi_pass_header Set-Cookie;
        fastcgi_pass_header Cookie;
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_cache_valid 404 60m;
        fastcgi_cache_valid 200 60m;
        fastcgi_max_temp_file_size 4m;
        fastcgi_cache_use_stale updating;
    }

    listen 80;
    listen 443 ssl;
}

please pay attention, your WordPress admin URL will be:

https://blog.example.com

while the blog front will be accessible via

https://www.example.com/blog

so you need to add this line of codes to the WordPress wp-config.php file

define( 'WP_HOME', 'https://www.example.com/blog' );
define( 'WP_SITEURL', 'https://blog.example.com' );

Known issue: With this configuration, you can't use WordPress templates that are based on elementor page builder, because when you try to edit a page in the admin, as the elementor is loaded in the front, you will get a cookie error, I couldn't fix this yet, but with non elementor templates like the Jannah theme, it's fine and there is no problem with the configuration

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.