【问题标题】:URL Rewrite to Remove .php in my Nginx PHP-FPMURL 重写以在我的 Nginx PHP-FPM 中删除 .php
【发布时间】:2020-06-08 19:07:44
【问题描述】:

我用 Centos 7 和 Virtualmin 控制面板设置了 Nginx PHP-FPM。我希望所有页面都有一个没有 .php 的 SEO 友好链接。

系统自动创建配置:etc/nginx/nginx.conf

还有空文件夹: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;


    }


}

我发现很多信息包括下面的代码使 url 在没有 .php 的情况下工作 但是应用之后,页面只是显示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;
}

我的问题是,我应该在哪里插入上面的代码?什么是完整的代码,所以它会工作。可能是我错过了一些参数。如果我必须在etc/nginx/conf.d/ 中创建一个 .conf 文件,我必须在 conf 文件中放入的完整代码是什么?

非常感谢任何帮助。 (注意My.Domain.IP.Address是IP地址,mydomain是网站域名)。

【问题讨论】:

  • 可能您问题中的配置工作正常,尽管带有.php 扩展名。查看生成 404 响应的访问日志和错误日志条目以及进行更改之后的配置会很有帮助。
  • Richard Smith,是的,使用 .php。但是在使用 url rewrite 进行配置和测试时,没有任何 404 错误日志。只有这个错误出现:"conflicting server name "www.mydomain.com" on My.Domain.IP.Address:80,忽略” 刚刚做了最后一次测试,它似乎开始工作了。通过将 seo 友好代码放在上面:listen My.Domain.IP.Address:443 default ssl;距离 nginx.conf 底部仅几行但仍然存在冲突的服务器名称错误。解决办法是什么?
  • 只需添加一个更正。我在“fastcgi_read_timeout 30;”下添加了代码。也在工作。

标签: php nginx url-rewriting centos7 fpm


【解决方案1】:

我的问题是,我应该在哪里插入上面的代码?

您域的server 块包含一个location 块。另外两个location 块需要添加到同一个server 块中。

例如:

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;
    }

    ...
}

但是仍然存在冲突的服务器名称错误。解决办法是什么?

使用nginx -T(即大写的T)测试Nginx 配置文件查看所有包含文件的完整配置。检查server_name 指令并确定重复名称的来源。

仅仅因为您的发行版包含include 指令和目录来帮助跨多个文件组织您的配置,您不需要使用它们。将整个配置保存在单个 nginx.conf 中可以方便更简单的服务器。

【讨论】:

  • 谢谢。对于 .php 没有工作的 seo 友好,但我只是与放置在下面的任何代码混淆:listen 80 default_server;服务器块,根本不工作。在这种情况下,我将 https 重定向不起作用:return 301 https://$host$request_uri;
【解决方案2】:

您好,进行以下修改

从您的代码中删除:

@extensionless-php;

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

.php$ {

【讨论】:

  • 对不起,我没看懂,能不能写个完整的代码?
【解决方案3】:
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;
    }

【讨论】:

  • 谢谢,留作参考。
猜你喜欢
  • 1970-01-01
  • 2014-02-18
  • 2020-04-06
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多