Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

前言

搭建Nextcloud云同步项目,项目域名URL中有“index.php”字符,影响URL的美观,从用户体验的角度来看,一个简洁、没有多余字符的URL会显得更专业、更直观。

Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

搜索引擎通常更倾向于简洁、有意义的URL,移除“index.php”可以使网页的URL更符合搜索引擎的喜好,有助于提高网站在搜索结果中的排名。简洁的URL还能减少重复内容的可能性,避免因为URL结构复杂而导致的搜索引擎误判。

Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

操作步骤

一、按照下图在宝塔面板的文件管理功能,找到Nextcloud使用的PHP对应版本配置文件(路径:/www/server/nginx/conf),例如我搭建的Nextcloud使用的PHP版本是8.2,在conf目录里找到enable-php-82.conf,双击此文件。

Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

二、按照下图在配置文件中添加以下代码并保存文件。

fastcgi_param front_controller_active true;

Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

三、在conf目录中双击打开mime.types文件。

Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

四、按照下图添加以下代码并保存文件。

 application/javascript                           mjs;

Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

五、在网站设置的伪静态规则添加以下代码并保存配置。

location ~ ^/.well-known/carddav {
    return 301 $scheme://$host/remote.php/dav;
}
 
location ~ ^/.well-known/caldav {
    return 301 $scheme://$host/remote.php/dav;
}
 
location /.well-known {
    rewrite ^/.well-known/webfinger /index.php/.well-known/webfinger last;
    rewrite ^/.well-known/nodeinfo /index.php/.well-known/nodeinfo last;
    rewrite ^/.well-known/(.*)$ /index.php/.well-known/$1 last;
} 
 
# 处理 Nextcloud 的 ocm-provider 和 ocs-provider
location ^~ /ocm-provider/ {
    try_files $uri $uri/ /index.php$request_uri;
}
 
location ^~ /ocs-provider/ {
    try_files $uri $uri/ /index.php$request_uri;
}
#webdav和其他所有请求重定向到index.php上
location / {
    rewrite ^ /index.php$uri;
    rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
    rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
    rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
    rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
    }
#静态资源重定向
location ~* \.(?:png|html|ttf|ico|jpg|jpeg)$ {
    try_files $uri /index.php$uri$is_args$args;
    access_log off;
}
location ~ ^/(?:updater|ocs-provider)(?:$|/) {
    try_files $uri/ =404;
    index index.php;
}
#安全设置,禁止访问部分敏感内容
location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
    deny all;
}
location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) {
    deny all;
}
location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
    deny all;
}
location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
    try_files $uri/ =404;
    index index.php;
}
# Adding the cache control header for js, css and map files
# Make sure it is BELOW the PHP block
location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
    try_files $uri /index.php$request_uri;
    add_header Cache-Control "public, max-age=15778463";
    add_header Referrer-Policy "no-referrer" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Download-Options "noopen" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Permitted-Cross-Domain-Policies "none" always;
    add_header X-Robots-Tag "none" always;
    add_header X-XSS-Protection "1; mode=block" always;
    # Optional: Don't log access to assets
    access_log off;
}
location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap|mp4|webm)$ {
    try_files $uri /index.php$request_uri;
    # Optional: Don't log access to other assets
    access_log off;
}
#mjs报错
location ~* \.mjs$ {
    types { application/javascript mjs; }
    try_files $uri =404;
}
# WASM
location ~* \.wasm$ {
    types {
        application/wasm wasm;
    }
    add_header Content-Type application/wasm;
    expires 7d;
    access_log off;
}
# otf
location ~* \.otf$ {
    types {
        application/x-font-otf otf;
        font/opentype otf;
    }
    add_header Access-Control-Allow-Origin *;
    expires 30d;
    access_log off;
}

六、打开Nextcloud的域名网址或分享链接,URL中没有index.php,这样更美观。

给TA赏糖
共{{data.count}}人
人已赏糖
技术分享

六个步骤带你解除安全模式轻松解决电脑启动难题

2024-12-8 20:37:10

技术分享

简单代码禁用WordPress更新提示

2024-12-9 11:17:36

0 条回复 A文章作者 M管理员
技术宅评论
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索