如何在NGINX中插入自定义位置代码(尝试删除.php .html扩展名)

时间:2019-01-13 09:35:53

标签: nginx nginx-location nginx-config

嗨,我是编辑Nginx文件配置的新手,如果有人可以指出我必须在下面的配置中插入代码或为我插入代码并发布完整编辑后的内容来帮助我,那真是太棒了配置代码!

我正在尝试将https://domain.tld/contact.php(和.html)的URL重命名为https://domain.tld/contact

我需要插入的代码

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

我的http nginx配置文件

# Redirect www and non-www http to non-www https
server {
    listen IP:80;
    server_name www.example.com domain.com;
    return 301 https://example.com$request_uri;
}
server {
    listen      IP:80;
    server_name domain.com www.example.com;
    root        /home/admin/web/domain.com/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/domain.com.log combined;
    access_log  /var/log/nginx/domains/domain.com.bytes bytes;
    error_log   /var/log/nginx/domains/domain.com.error.log error;

    location / {

        location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
            expires     max;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }

            fastcgi_pass    127.0.0.1:9002;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

    location /error/ {
        alias   /home/user/web/domain.com/document_errors/;
    }

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }

    location /vstats/ {
        alias   /home/user/web/domain.com/stats/;
        include /home/user/conf/web/example.com.auth*;
    }

    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/user/conf/web/nginx.example.com.conf*;
}

几乎没有问题,因为我正在使用VESTACP来创建两个配置文件(http的nginx.conf和https的snginx.conf),我该编辑哪个文件?我将所有内容重定向到https非www。

我尝试将代码放置在nginx.example.com.conf中,该代码包含在我在此处发布的主要配置中,但是出现错误,并且我不知道该语法必须放在哪里语法访问配置。

0 个答案:

没有答案