nextcloud宝塔面板nginx伪静态-去除index.php

修改宝塔PHP配置文件<font color=red>【非常重要,必须要改,否则不生效!!!】</font>
  • 路径:/www/server/nginx/conf
  • 文件名:enable-php-74.conf 根据所使用php版本修改相对应文件
  •  location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi-74.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            include pathinfo.conf;
        }

    在配置文件最后一行加上fastcgi_param front_controller_active true;

    location ~ [^/]\.php(/|$)
    {
        try_files $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi-74.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
        include pathinfo.conf;
        fastcgi_param front_controller_active true;
    }

 

添加nginx伪静态规则和一些其它安全配置
  • 在宝塔面板的伪静态页面添加,也可直接在配置文件里面添加
  • #(&#x53EF;&#x9009;)&#x6DFB;&#x52A0;&#x5982;&#x4E0B;header&#x4E3B;&#x8981;&#x4E3A;&#x4E86;&#x5B89;&#x5168;
    add_header Strict-Transport-Security &quot;max-age=63072000;&quot;;
    #&#x89E3;&#x6790;caldav&#x548C;carddav
    rewrite /.well-known/carddav /remote.php/dav permanent;
    rewrite /.well-known/caldav /remote.php/dav permanent;
    #&#x9759;&#x6001;&#x8D44;&#x6E90;&#x91CD;&#x5B9A;&#x5411;1
    location ~* \/core\/(?:js\/oc\.js|preview\.png).*$ {
        rewrite ^ /index.php last;
    }
    #webdav&#x548C;&#x5176;&#x4ED6;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#x91CD;&#x5B9A;&#x5411;&#x5230;index.php&#x4E0A;
    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;
    #&#x9759;&#x6001;&#x8D44;&#x6E90;&#x91CD;&#x5B9A;&#x5411;2,&#x652F;&#x6301;&#x4F7F;&#x7528;acme&#x811A;&#x672C;&#x5728;&#x7533;&#x8BF7;&#x8BC1;&#x4E66;&#x65F6;&#x5BF9;&#x57DF;&#x540D;&#x7684;&#x9A8C;&#x8BC1;
        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;
        }
    }
    #&#x9759;&#x6001;&#x8D44;&#x6E90;&#x91CD;&#x5B9A;&#x5411;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&#x548C;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;
    }
    #(&#x53EF;&#x9009;)&#x4E3A;&#x4E86;&#x652F;&#x6301;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;
    #&#x652F;&#x6301;&#x65E5;&#x5386;&#x548C;&#x8054;&#x7CFB;&#x4EBA;&#xFF0C;&#x5EFA;&#x8BAE;&#x52A0;&#x4E0A;
    location = /.well-known/carddav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host:$server_port/remote.php/dav;
    }
    #&#x542F;&#x52A8;Gzip,&#x4E0D;&#x8981;&#x5220;&#x9664;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;
    #&#x5B89;&#x5168;&#x8BBE;&#x7F6E;&#xFF0C;&#x7981;&#x6B62;&#x8BBF;&#x95EE;&#x90E8;&#x5206;&#x654F;&#x611F;&#x5185;&#x5BB9;
    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;
    }
    #&#x8FD9;&#x90E8;&#x5206;&#x5427;&#xFF0C;&#x9ED8;&#x8BA4;&#x5C31;&#x6709;&#xFF0C;&#x4E0D;&#x8FC7;&#x6709;&#x6240;&#x4E0D;&#x540C;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x5408;&#x5E76;&#x4E86;&#x4E0B;&#xFF0C;&#x66FF;&#x6362;&#x539F;&#x6765;&#x7684;&#x5C31;&#x884C;
    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-74.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 &quot;public, max-age=15778463&quot;;
        add_header Referrer-Policy &quot;no-referrer&quot; always;
        add_header X-Content-Type-Options &quot;nosniff&quot; always;
        add_header X-Download-Options &quot;noopen&quot; always;
        add_header X-Frame-Options &quot;SAMEORIGIN&quot; always;
        add_header X-Permitted-Cross-Domain-Policies &quot;none&quot; always;
        add_header X-Robots-Tag &quot;none&quot; always;
        add_header X-XSS-Protection &quot;1; mode=block&quot; always;
        # Optional: Don&apos;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&apos;t log access to other assets
        access_log off;
    }

发表评论

邮箱地址不会被公开。 必填项已用*标注

click to changeSecurity Code