Nginx

Nginx http 到 https 重定向下載空文件

  • February 2, 2022

我試圖將我的Laravel+vuejs+nuxtjs項目從http重定向到https,但是當我輸入**http://example.com>**或**<http://www.example.com**時,會下載一個空文件

到目前為止我做了什麼:

1-在 nginx.conf 中default_type application/octet-stream添加註釋和default_type text/html代替

2-定義types { } default_type "text/plain";example.com.conflocation /{}

3-nginx 使用下面的程式碼重定向

server{
 listen xx.xx.xx.xx:80;
   server_name example.com www.example.com;
 return 301 https://www.example.com$request_uri;
}

4-嘗試使用帶有以下 example.com.conf 文件的 .php 文件重定向它:

      server {
         listen 37.152.191.249:80;
         server_name www.example.com example.com;

        access_log /usr/local/apache/domlogs/example.com.bytes bytes;
       access_log /usr/local/apache/domlogs/example.com.log combined;
       error_log /usr/local/apache/domlogs/example.com.error.log error;

       root /home/example/public_html/;
       index index.php;

  location / {
types { } default_type "text/plain";
   try_files                       $uri $uri/  /index.php?$query_string;

}

location ~ \.php$ {
  fastcgi_split_path_info         ^(.+\.php)(/.+)$;
   fastcgi_pass                    127.0.0.1:9000;
   fastcgi_index                   index.php;
   include                         fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_intercept_errors        off;
   fastcgi_buffer_size             16k;
   fastcgi_buffers                 4 16k;
   fastcgi_connect_timeout         300;
  fastcgi_send_timeout            300;
   fastcgi_read_timeout            300;
}


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

       disable_symlinks if_not_owner from=/home/example/public_html;
}

public_html 程式碼中的 index.php :

   $location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   header('HTTP/1.1 301 Moved Permanently');
   header('Location: ' . $location);
   exit;

以上都沒有奏效,問題仍然存在。

+目前配置:

nginx -t 報告:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

example.com.ssl.conf:

server{
 listen xx.xx.xx.xx:443 http2 ssl;
   server_name example.com;
       ssl_certificate      /etc/pki/tls/certs/example.com.bundle;
       ssl_certificate_key  /etc/pki/tls/private/example.com.key;
       ssl_protocols TLSv1.2;
       ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EE3CDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA!RC4:EECDH:!RC4:!aNULL:!eN$
       ssl_prefer_server_ciphers   on;

      ssl_session_cache   shared:SSL:10m;
      ssl_session_timeout 60m;
      return 301 https://www.example.com$request_uri;
}
      server {
         listen xx.xx.xx.xx:443 http2 ssl;
         server_name www.example.com;

        access_log /usr/local/apache/domlogs/example.com.bytes bytes;
       access_log /usr/local/apache/domlogs/example.com.log combined;
       error_log /usr/local/apache/domlogs/example.com.error.log error;
       ssl_certificate      /etc/pki/tls/certs/example.com.bundle;
       ssl_certificate_key  /etc/pki/tls/private/example.com.key;
       ssl_protocols TLSv1.2;
       ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA!RC4:EECDH:!RC4:!aNULL:!eN$
       ssl_prefer_server_ciphers   on;

       ssl_session_cache   shared:SSL:10m;
       ssl_session_timeout 60m;

       root /home/example/core/public/;
       index index.php;

  location / {
 proxy_set_header                Connection 'upgrade';
   proxy_http_version              1.1;
   proxy_pass                      https://xx.xx.xx.xx:3000$uri;
   proxy_intercept_errors          on;# In order to use error_page directive this needs to be on
   error_page                      404 = @php;
}

location @php {
   try_files                       $uri $uri/  /index.php?$query_string;

}

location ~ \.php$ {
  fastcgi_split_path_info         ^(.+\.php)(/.+)$;
   fastcgi_pass                    127.0.0.1:9000;
   fastcgi_index                   index.php;
   include                         fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_intercept_errors        off;
   fastcgi_buffer_size             16k;
   fastcgi_buffers                 4 16k;
   fastcgi_connect_timeout         300;
  fastcgi_send_timeout            300;
   fastcgi_read_timeout            300;
}


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

       disable_symlinks if_not_owner from=/home/example/public_html;

       location /.well-known/acme-challenge {
               default_type "text/plain";
               alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
       }

       location /.well-known/pki-validation {
               default_type "text/plain";
               alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
       }
}

目前 example.com.conf

server{
 listen xx.xx.xx.xx:80;
   server_name example.com www.example.com;
 return 301 https://www.example.com$request_uri;
}

我沒有添加 nginx -T 報告,因為它顯示了來自其他網站的不相關配置文件。

此外,執行多個站點的伺服器和 wordpress 使用在#3處提供的程式碼進行重定向也沒有問題,但是當涉及到使用 nuxtjs 的站點時,我下載了一個空文件**。**

任何幫助將不勝感激

我複制了我的一個 wordpress nginx 配置,並用域名替換了xxx部分****,用伺服器IP替換了****yyy

server {
       listen yyy.yyy.yyy.yyy:80;
       server_name xxx.com  www.xxx.com;
 return 301 https://xxx$request_uri;

       location / {
               try_files $uri $uri/ /index.php?$args;
               add_header Strict-Transport-Security "max-age=31536000";
               add_header X-XSS-Protection "1; mode=block" always;
               add_header X-Content-Type-Options "nosniff" always;

               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:9000;
                       fastcgi_index   index.php;
                       include         /etc/nginx/fastcgi_params;
               }

       }

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


       disable_symlinks if_not_owner from=/home/domainname/public_html;

       location /.well-known/acme-challenge {
               default_type "text/plain";
               alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
       }

       location /.well-known/pki-validation {
               default_type "text/plain";
               alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
       }
}

它無需下載任何東西就被重定向到 https,我已經將我對 nginx 和 php-fpm 所做的所有更改恢復為預設值。

現在它的工作但為什麼?

在玩耍後注意到將工作 nginx 配置的位置更改為location ~ \.php$(我之前使用的那個)將使問題返回。就是這樣!

沒有進一步調查為什麼location ~ \.php$不起作用但確實起作用。location ~ [^/]\.php(/|$)

希望能幫助到你。

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