无法使用nginx

时间:2019-10-10 05:26:32

标签: http ubuntu ssl nginx https

我在Laravel Web应用程序上实现了google api,只有用户在域名前手动输入https(这意味着我已加密SSL证书)时,地理位置才能起作用。我已经尝试过在这里发布的各种解决方案,在不同的在线教程中以及在数字海洋社区论坛上,都没有得到我想要的简单结果。

目前如何工作:

  • 如果用户输入www.domain.com或domain.com,则地理位置不起作用。
  • 如果用户在www.domain.com或domain.com之前手动输入https,则地理位置可以正常工作。

我当前的nginx配置代码:

server {
    listen 80;
    root /var/www/domain/public;
    index index.php index.html index.htm index.nginx-debian.html;
    server_name www.domain.com domain.com;

    location / {
            try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
            deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

我尝试过的事情:

我得到的结果是

  • “页面无法正确重定向”(Firefox)/“重定向太多”(chrome)
  • 默认为“欢迎使用nginx!”问候页面(将我当前的conf文件分为2个服务器块时发生)

任何帮助将不胜感激,在此先感谢您!

解决方案

因此,感谢这篇Nginx git server returns error 500的相关文章,我得以将配置文件修改为如下所示:

server {
    listen 80;
    server_name www.domain.com domain.com;
    rewrite ^ https://$server_name$request_uri? permanent;
} 

server {
    listen 443 ssl;
    server_name www.domain.com domain.com;
    root /var/www/domain/public;
    index index.php index.html index.htm index.nginx-debian.html;

    location / {
            try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
            deny all;
    }

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

希望这有助于其他人在LEMP服务器上运行的Laravel项目中工作:)

0 个答案:

没有答案