Linux

在同一主機和同一埠上的 NGINX 中使用多個伺服器塊

  • February 5, 2022

我想配置伺服器,以便伺服器的根目錄提供一些靜態文件,這些文件是特定的端點,/nextcloud 為同一域上的 nextcloud 提供服務。

這是我的 nginx.conf -

worker_processes  8;

events {
   worker_connections  1024;
}

http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;

   server {
       listen       80 http2;
       listen [::]:80 http2;
       server_name  localhost;
       root   /srv/http/;
       location / {
           index  index.html index.php;
           try_files $uri $uri/ =404;
           autoindex on;
       }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   /usr/share/nginx/html;
       }
   }
   include /etc/nginx/sites-enabled/*;
}

這是我的 /etc/nginx/sites-enabled/nextcloud.conf

upstream php-handler {
   server unix:/run/nextcloud/nextcloud.sock;
}

server {
   listen 80;
   listen [::]:80;
   server_name nextcloud;

   root /usr/share/webapps/;

   location = /robots.txt {
       allow all;
       log_not_found off;
       access_log off;
   }

   location ^~ /.well-known {
       location = /.well-known/carddav { return 301 /nextcloud/remote.php/dav/; }
       location = /.well-known/caldav  { return 301 /nextcloud/remote.php/dav/; }

       location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
       location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

       return 301 /nextcloud/index.php$request_uri;
   }

   location ^~ /nextcloud {
       client_max_body_size 512M;
       client_body_timeout 300s;
       fastcgi_buffers 64 4K;

       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/wasm 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;

       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;

       fastcgi_hide_header X-Powered-By;

       index index.php index.html /nextcloud/index.php$request_uri;

       location = /nextcloud {
           if ( $http_user_agent ~ ^DavClnt ) {
               return 302 /nextcloud/remote.php/webdav/$is_args$args;
           }
       }

       location ~ ^/nextcloud/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)    { return 404; }
       location ~ ^/nextcloud/(?:\.|autotest|occ|issue|indie|db_|console)                  { return 404; }

       location ~ \.php(?:$|/) {
           rewrite ^/nextcloud/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy) /nextcloud/index.php$request_uri;

           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 modHeadersAvailable true;         # Avoid sending the security headers twice
           fastcgi_param front_controller_active true;     # Enable pretty urls
           fastcgi_pass php-handler;

           fastcgi_intercept_errors on;
           fastcgi_request_buffering off;

           fastcgi_max_temp_file_size 0;
       }

       location ~ \.(?:css|js|svg|gif|png|jpg|ico|wasm|tflite)$ {
           try_files $uri /nextcloud/index.php$request_uri;
           expires 6M;         # Cache-Control policy borrowed from `.htaccess`
           access_log off;     # Optional: Don't log access to assets

           location ~ \.wasm$ {
               default_type application/wasm;
           }
       }

       location ~ \.woff2?$ {
           try_files $uri /nextcloud/index.php$request_uri;
           expires 7d;         # Cache-Control policy borrowed from `.htaccess`
           access_log off;     # Optional: Don't log access to assets
       }

       location /nextcloud/remote {
           return 301 /nextcloud/remote.php$request_uri;
       }

       location /nextcloud {
           try_files $uri $uri/ /nextcloud/index.php$request_uri;
       }
   }
}

問題是這個配置不起作用。使用此配置,當我嘗試訪問 /nextcloud/ 時得到 404。

如果我在 nginx.conf 中禁用靜態文件伺服器塊,我可以訪問 /nextcloud/,但是我無法訪問我的靜態文件。如何配置使兩者都在同一主機和同一埠上工作?

您需要在同server一塊中配置兩者。

當 nginx 收到請求時,它會根據Host請求中的 HTTP 標頭和埠號來選擇要使用的虛擬主機。

nextcloud如果您使用 訪問該服務,則您目前的配置將起作用http://nextcloud/nextcloud,前提是您已為nextcloud名稱正確配置了 DNS。

你可能想要在你的 nginx 配置中是這樣的:

worker_processes  8;

events {
   worker_connections  1024;
}

http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;

   server {
       listen       80 http2;
       listen [::]:80 http2;
       server_name  localhost;
       root   /srv/http/;
       location / {
           index  index.html index.php;
           try_files $uri $uri/ =404;
           autoindex on;
       }
       
       location /nextcloud {
           ...
       }

       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   /usr/share/nginx/html;
       }
   }
   include /etc/nginx/sites-enabled/*;
}

並刪除其他配置。

引用自:https://serverfault.com/questions/1092763