2

I setup Nginx PHP-FPM, with Centos 7, and Virtualmin control panel. I want to have all pages a SEO Friendly link without .php.

System automatically create configuration : etc/nginx/nginx.conf

And empty folder: etc/nginx/conf.d/

nginx.conf :

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;


        # Load configuration files for the default server block.
          include /etc/nginx/default.d/*.conf;

           location / {
           }

        #error_page 404 /404.html;
           location = /40x.html{
       }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }         
    }




# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

    server_names_hash_bucket_size 128;
    server {
        server_name mydomain.com www.mydomain.com;
        listen My.Domain.IP.Address;
        root /home/mydomain/public_html;
        index index.html index.htm index.php;        
        access_log /var/log/virtualmin/mydomain.com_access_log;
        error_log /var/log/virtualmin/mydomain.com_error_log;       
        fastcgi_param GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param SERVER_SOFTWARE nginx;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_param SCRIPT_FILENAME /home/mydomain/public_html$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT /home/mydomain/public_html;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param REMOTE_ADDR $remote_addr;
        fastcgi_param REMOTE_PORT $remote_port;
        fastcgi_param SERVER_ADDR $server_addr;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_NAME $server_name;
        fastcgi_param HTTPS $https;
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass localhost:8000;
        }
        listen My.Domain.IP.Address:443 default ssl;
        ssl_certificate /home/mydomain/ssl.combined;
        ssl_certificate_key /home/mydomain/ssl.key;
        fastcgi_read_timeout 30;


    }


}

I found many information include the code below makes url working without .php But after applying that, the page just shown 404 not found page.

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    try_files $uri =404;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

My question is, where should I insert the code above? what is completed code so it will work. May be I miss some parameter. If I must create a .conf file in etc/nginx/conf.d/ what is the complete code that i must put in the conf file?

Any help is much appreciated. (please note that My.Domain.IP.Address is replace with an IP address, and mydomain is a website domain name.).

Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
Ray
  • 35
  • 5
  • Presumably the configuration in your question is working correctly albeit with a `.php` extension. It would be helpful to see the access log and error log entries which genertate the 404 response and the configuration **after** you make the changes. – Richard Smith Feb 25 '20 at 10:17
  • Richard Smith , Yes working with .php. But when doing configuration and testing with url rewrite, there is no any error log with 404. Only this error come up : **"conflicting server name "www.mydomain.com" on My.Domain.IP.Address:80, ignored"** Just did last test, it seem start to work. By placing the seo freindly code just above : listen My.Domain.IP.Address:443 default ssl; Just several line from bottom of nginx.conf But conflicting server name error still exist. What is the solution? – Ray Feb 25 '20 at 12:47
  • Just add a correction. I added the code just under "fastcgi_read_timeout 30;". is working too . – Ray Feb 28 '20 at 15:54

3 Answers3

0

My question is, where should I insert the code above?

The server block for your domain contains one location block. The other two location blocks need to be added to that same server block.

For example:

server {
    server_name example.com www.example.com;
    root /home/mydomain/public_html;

    ...

    location / {
        try_files $uri $uri.html $uri/ @extensionless-php;
        index index.html index.htm index.php;
    }    
    location @extensionless-php {
        rewrite ^ $1.php last;
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass localhost:8000;
    }

    ...
}

But conflicting server name error still exist. What is the solution?

Use nginx -T (that's an uppercase T) to test the Nginx configuration file and view the entire configuration across all the included files. Inspect the server_name directives and identify where the duplicated name is coming from.

Just because your distribution includes include directives and directories to help organise your configuration across multiple files, you do not need to use them. Keeping the entire configuration in a single nginx.conf can be convenient for simpler servers.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Thank you. For seo friendly without .php already working, but l just confused with any code that are placed under : listen 80 default_server; server block, not working at all. In this case i place https redirect not working : return 301 https://$host$request_uri; – Ray Feb 25 '20 at 19:21
0

Hello make the following modifications

remove from your code:

@extensionless-php;

 location @extensionless-php {
       rewrite ^ $1.php last;
   }

.php$ {
Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29
0
server {
server_name example.com www.example.com;
root /home/mydomain/public_html;

...
location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't 
break when using query string
            try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass localhost:8000;
    }
Paulo Boaventura
  • 1,365
  • 1
  • 9
  • 29