• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved SEO-friendly URLs after switch from Apache to nginx

pec

New Pleskian
Hi,

I am currently trying to switch from Apache to nginx.
As I do not have experience with nginx I thought it would be good idea to start with my dev sub domain.
First, I enabled nginx as described in https://support.plesk.com/hc/en-us/articles/213944825-How-to-enable-Nginx-reverse-proxy-in-Plesk. Verything went well, nginx conf files were created automatically.
Second, I deactivated the proxy mode for this sub-domain. nginx conf files were updated automatically.

Now I am struggling a bit with the SEO-frienldy URLs that worked well on Apache.
I am running a XenForo board. As described here I added the given code in the "Additional nginx directives" section of the sub-domain settings.

Now the home page is ok, but all other links do not work, answering with a 404 for a missing "index.html".
So, nginx doesn't translate the request, but always searches for an index.hml.
E.g. a request for a post https://dev.domain.com/posts/9876 is not answered with https://dev.domain.com/threads/title-of-the-thread.123/page-3#post-9876 but with a missing https://dev.domain.com/posts/9876/index.html.

That's the nginx.conf created by plesk:
Code:
#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.

server {
    listen 1.2.3.4:443 ssl;

    server_name dev.domain.com;
    server_name www.dev.domain.com;
    server_name ipv4.dev.domain.com;

    ssl_certificate             /opt/psa/var/certificates/certjdLQBMx;
    ssl_certificate_key         /opt/psa/var/certificates/certjdLQBMx;

    client_max_body_size 128m;

    root "/var/www/vhosts/domain.com/httpdocs/dev";
    access_log "/var/www/vhosts/system/dev.domain.com/logs/proxy_access_ssl_log";
    error_log "/var/www/vhosts/system/dev.domain.com/logs/proxy_error_log";

    location ~ ^/plesk-stat/ {
        auth_basic "Domain statistics";
        auth_basic_user_file "/var/www/vhosts/system/dev.domain.com/pd/d..httpdocs@plesk-stat";

        location ~ \.php(/.*)?$ {
            fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_pass "unix:///var/www/vhosts/system/dev.domain.com/php-fpm.sock";
            include /etc/nginx/fastcgi.conf;
        }

        location ~ /$ {
            index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
        }
    }

    location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
        alias /var/www/vhosts/domain.com/web_users/$1/$2;
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/dev.domain.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;
    }

    location ~ \.php(/.*)?$ {
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/dev.domain.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;
    }

    location ~ /$ {
        index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
    }

    include "/var/www/vhosts/system/dev.domain.com/conf/vhost_nginx.conf";
}
And that's what I added in the "Additional nginx directives" section:
Code:
location / {
    try_files $uri $uri/ /index.php?$uri&$args;
    index index.php index.html;
}

location /install/data/ {
    internal;
}
location /install/templates/ {
    internal;
}
location /(internal_data|library) {
    internal;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
}

The issue seem to be caused by
Code:
    location ~ /$ {
        index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
    }
in the sub-domain's nginx.conf. As soon as I remove this part everything works.
Unfortunately this is only a temporary solution. As stated in the file's header, it will come back :(

Any ideas how to fix this ?
 
Hi pec,

you might have noticed, that additional nginx directives ( placed inside the (sub)domain - specific "vhost_nginx.conf" ) could overwrite/modify existing configurations from your "nginx.conf".

Examples:

YOUR nginx.conf :
Code:
    location ~ \.php(/.*)?$ {
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/dev.domain.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;
    }
YOUR vhost_nginx.conf:
Code:
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
}
I would recommend to leave that out, first because your "fastcgi_pass" is not set to your actual php-handler - socket and second, because your "/etc/nginx/fastcgi.conf" already includes the setting:
Code:
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;



Next, keep an eye onto YOUR vhost_nginx.conf :
location / {
try_files $uri $uri/ /index.php?$uri&$args;
index index.php index.html;
}
I would strongly recommend to use this instead:
Code:
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php break;
        }



YOUR vhost_nginx.conf :
Code:
    location /install/data/ {
        internal;
    }
    location /install/templates/ {
        internal;
    }
    location /internal_data/ {
        internal;
    }
    location /library/ {
        internal;
    }
... should be o.k. and will not interfere with the nginx.conf.​
 
  • Like
Reactions: pec
you might have noticed, that additional nginx directives ( placed inside the (sub)domain - specific "vhost_nginx.conf" ) could overwrite/modify existing configurations from your "nginx.conf".
I thought as much and did a search on the internet, but didn't find something. Maybee I used the wrong search terms o_O

I would recommend to leave that out, first because your "fastcgi_pass" is not set to your actual php-handler - socket and second, because your "/etc/nginx/fastcgi.conf" already includes the setting:
As mentioned above, I am totally new to nginx. 10 years experience with Apache, but only 2 weeks with nginx ;)
That's why I copied the whole code snipped from the XenForo page.
Thank you for pointing this out.

I made all the changes you proposed and restartet nginx. The result is still the same: 404 :(
2017/03/14 20:09:53 [error] 28691#0: *527635 "/var/www/vhosts/domain.com/httpdocs/dev/forums/trainingsberichte.10/index.html" is not found (2: No such file or directory), client: xx.xxx.xxx.xxx, server: dev.domain.com, request: "GET /forums/trainingsberichte.10/ HTTP/1.1", host: "dev.domain.com", referrer: "https://dev.domain.com/"

The vhost_nginx.conf:
Code:
location / {
   if (!-e $request_filename){
       rewrite ^(.*)$ /index.php break;
       }
}

location /install/data/ {
    internal;
}
location /install/templates/ {
    internal;
}
location /(internal_data|library) {
    internal;
}
 
location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php break; } }
Pls. JUST the suggestions... nothing more:

Code:
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php break;
        }
 
  • Like
Reactions: pec
Pls. JUST the suggestions... nothing more:
Sorry. As it was tabbed I thought it has to be within the location part.
Works now ;)

One last question: after depoying all the changes from the DEV to the PROD system, my munin monitoring isn't accessable anymore.
The munin files are stored in /var/www/munin.
As suggested here https://www.scalescale.com/tips/nginx/monitor-nginx-munin/ I set the following in the vhost_nginx.conf for the default domain:
Code:
# munin configuration
location /munin {
    alias   /var/www/munin/;
    index  index.php index.html index.htm;
    location ~* \.(png|jpg|jpeg|gif|ico)$ {
    }
}

Nevertheless, I am not able to access the munin stats, neither via deafult domain nor via IP of the server.
The XenForo software steps in and complains about a missing route to "/munin".

Complete vhost_nginx.conf now looks like this:
Code:
# munin configuration
location /munin {
    alias   /var/www/munin/;
    index  index.php index.html index.htm;
    location ~* \.(png|jpg|jpeg|gif|ico)$ {
    }
}

if (!-e $request_filename){
    rewrite ^(.*)$ /index.php break;
}

location /install/data/ {
    internal;
}
location /install/templates/ {
    internal;
}
location /(internal_data|library) {
    internal;
}

# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|gz|svg|svgz|mp4|ogg|ogv|webm|htc|woff)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
}
 
Hi pec,

even that it may sound strange for you, but you should consider to open a new thread, if you would like help for your additional "munin" - question, because it has got nothing to do with your thread title and the initial start post.
 
  • Like
Reactions: pec
Back
Top