Score:0

Nginx - same server, one specific path to a different root folder

au flag

I am having a hard time trying to figure out how to configure Nginx. Basically, I have the same server name, but two websites:

  1. website x root folder: /var/www/html/x/public
  2. Wordpress root folder: /var/www/html/wordpress

When I access localhost:8443 it will go to: /var/www/html/x/public when I access localhost:8443/blog it will go to: /var/www/html/wordpress

I managed to make it work up to a certain point where localhost:8443/blog works but localhost:8443/blog/my-blog-post doesn´t.

server {
    listen [::]:443 ssl http2 ipv6only=on;
    listen 443 ssl http2;
    server_name localhost;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";    

    charset utf-8;   
    
    index index.php;  
    root /var/www/html/exploraai/public; 

    location ^~ /blog {            
        alias /var/www/html/blog;  

        location ~ \.php$ {
            try_files $uri = 404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }   
    }  

    location / {  
        try_files $uri $uri/ /index.php?$query_string;     
    }

    location ~ \.php$  {                 
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }   

    location ~ /\.(?!well-known).* {
        deny all;
    }

    location = /favicon.ico { 
        access_log off; log_not_found off; 
    }
    location = /robots.txt  { 
        access_log off; log_not_found off; 
    }

}

I tried different things, but so far nothing worked.

Update

What worked for me was replacing the location blog for the following location block

location /blog {        
    alias /var/www/html/blog/;  

    try_files $uri /blog/index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }   
} 

in flag
You're going to want to confirm the URL in the WordPress database matches what you need it to be for nginx. WordPress is rather sensitive about how it renders and handles URLs.
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.