AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1090937
Accepted
Pc Monk
Pc Monk
Asked: 2022-01-24 22:07:34 +0800 CST2022-01-24 22:07:34 +0800 CST 2022-01-24 22:07:34 +0800 CST

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

  • 772

我试图将我的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 的站点时,我下载了一个空文件。

任何帮助将不胜感激

nginx centos7 301-redirect
  • 1 1 个回答
  • 274 Views

1 个回答

  • Voted
  1. Best Answer
    Pc Monk
    2022-02-03T00:16:00+08:002022-02-03T00:16:00+08:00

    我复制了我的一个 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(/|$)

    希望能帮助到你。

    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve