不凡博客

版权声明

未经允许,请勿转载本博客的文章、图片和视频,谢谢!
首页 > 建站 > 正文

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

建站 | 2024年05月25日16:03:05

腾讯云高性价比国内轻量服务器,2核2G4M,99元/年,新老用户同享,续费同价

前言

  搭建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/webfinger {
    return 301 $scheme://$host/index.php/.well-known/webfinger;
}

location = /.well-known/nodeinfo {
    return 301 $scheme://$host/index.php/.well-known/nodeinfo;
}
# 处理 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;
}

# 确保 PHP 处理被正确设置
location ~ \.php(?:$|/) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # 确认你的PHP-FPM路径
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS on;
}

#解析caldav和carddav
rewrite /.well-known/carddav /remote.php/dav permanent;
rewrite /.well-known/caldav /remote.php/dav permanent;
#静态资源重定向1
location ~* \/core\/(?:js\/oc\.js|preview\.png).*$ {
    rewrite ^ /index.php last;
}
#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;
#静态资源重定向2,支持使用acme脚本在申请证书时对域名的验证
    if ($uri !~* (?:\.(?:css|js|svg|gif|png|html|ttf|woff)$|^\/(?:remote|public|cron|status|ocs\/v1|ocs\/v2)\.php|^\/\.well-known\/acme-challenge\/.*$)){
        rewrite ^ /index.php last;
    }
}
#静态资源重定向3
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;
}
#caldav和carddav
rewrite /.well-known/carddav /remote.php/dav permanent;
rewrite /.well-known/caldav /remote.php/dav permanent;
# Remove X-Powered-By, which is an information leak
fastcgi_hide_header X-Powered-By;

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}
#(可选)为了支持user_webfinger app
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
#启动Gzip,不要删除ETag headers
gzip on;
gzip_vary on;
gzip_comp_level 4;
gzip_min_length 256;
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
#安全设置,禁止访问部分敏感内容
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;
}
#这部分吧,默认就有,不过有所不同,所以我合并了下,替换原来的就行
location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy)\.php(?:$|\/) {
    fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
    set $path_info $fastcgi_path_info;
    try_files $fastcgi_script_name =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $path_info;
    fastcgi_param HTTPS on;
    fastcgi_pass unix:/tmp/php-cgi-82.sock;
    # Avoid sending the security headers twice
    fastcgi_param modHeadersAvailable true;
    # Enable pretty urls
    fastcgi_param front_controller_active true;
    fastcgi_intercept_errors on;
    fastcgi_request_buffering off;
}
# 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;
}

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

六、如果你的Nextcloud使用的PHP版本号不是8.2,在伪静态规则中找到php8.2(第28行),修改成你的Nextcloud使用的PHP版本号并保存配置。

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

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

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

The End
淘宝购物先领券,更省钱

本文标题:Nextcloud在宝塔面板和Nginx环境移除URL中的index.php

本文链接:https://www.bufanz.com/post/65.html

版权声明:本文章是 不凡博客 的原创文章,未经允许请勿转载本文章!