Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ubuntu nginx将非www重定向到www和https_Ubuntu_Redirect_Ssl_Nginx_Web - Fatal编程技术网

Ubuntu nginx将非www重定向到www和https

Ubuntu nginx将非www重定向到www和https,ubuntu,redirect,ssl,nginx,web,Ubuntu,Redirect,Ssl,Nginx,Web,以下是我的nginx配置代码: server { listen 80; server_name example.com www.example.com; return 301 https://www.example.com$request_uri; } server { listen 443 ssl; server_name example.com; ssl_certificate /etc/ssl/example.com.crt; ss

以下是我的nginx配置代码:

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

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /etc/ssl/example.com.crt;
    ssl_certificate_key /etc/ssl/example.com.key;
    return 301 https://www.example.com$request_uri;
}


server {
    listen 443 ssl;
    server_name www.example.com;
    ssl_certificate /etc/ssl/example.com.crt;
    ssl_certificate_key /etc/ssl/example.com.key;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #   proxy_pass http://127.0.0.1:8080;    
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
    #location = /50x.html {
    #   root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #   # With php5-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php5-fpm:
    #   fastcgi_pass unix:/var/run/php5-fpm.sock;
    #   fastcgi_index index.php;
    #   include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}
以下是我想做的:

  • 强制非www到www
  • 强制http到https
  • 最终结果将是

我把它放在默认的配置文件中,但它不起作用。我得到的错误是重定向没有正确重定向。有人能帮忙吗?谢谢

您将需要以下服务器块:

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

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

您将需要以下服务器块:

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

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

1sloc anwser对我来说不起作用。如果访客去参观怎么办

编辑:我刚刚意识到我几乎和你有相同的配置,为什么它不适合你

我使用以下配置

# Redirect http traffic to https://www.
server {
  listen [::]:80;
  listen 80;

  server_name example.com www.example.com;

  return 301 https://www.example.com$request_uri;
}

# Redirect https traffic to https://www.
server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    # listen on the wrong host
    server_name example.com;

    # Paths to SSL certificate files.
    ssl_certificate         /etc/ssl/ssl-example.com.crt;
    ssl_certificate_key     /etc/ssl/ssl-example.com.key;
    ssl_trusted_certificate /etc/ssl/ssl-example.com.crt;

    ssl_dhparam         /etc/ssl/ssl-example.com.pem;

    # Include defaults for allowed SSL/TLS protocols and handshake caches.
    include /etc/nginx/custom/ssl.conf;


    # and redirect to the non-www host (declared below)
    return 301 https://www.example.com$request_uri;
}

# Main https://www. server block
server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    # listen on the wrong host
    server_name example.com;

    # Paths to SSL certificate files.
    ssl_certificate         /etc/ssl/ssl-example.com.crt;
    ssl_certificate_key     /etc/ssl/ssl-example.com.key;
    ssl_trusted_certificate /etc/ssl/ssl-example.com.crt;

    ssl_dhparam         /etc/ssl/ssl-example.com.pem;

    # Include defaults for allowed SSL/TLS protocols and handshake caches.
    include /etc/nginx/custom/ssl.conf;

    root /data/sites/example.com/;
    index index.html index.phtml;

}

1sloc anwser对我来说不起作用。如果访客去参观怎么办

编辑:我刚刚意识到我几乎和你有相同的配置,为什么它不适合你

我使用以下配置

# Redirect http traffic to https://www.
server {
  listen [::]:80;
  listen 80;

  server_name example.com www.example.com;

  return 301 https://www.example.com$request_uri;
}

# Redirect https traffic to https://www.
server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    # listen on the wrong host
    server_name example.com;

    # Paths to SSL certificate files.
    ssl_certificate         /etc/ssl/ssl-example.com.crt;
    ssl_certificate_key     /etc/ssl/ssl-example.com.key;
    ssl_trusted_certificate /etc/ssl/ssl-example.com.crt;

    ssl_dhparam         /etc/ssl/ssl-example.com.pem;

    # Include defaults for allowed SSL/TLS protocols and handshake caches.
    include /etc/nginx/custom/ssl.conf;


    # and redirect to the non-www host (declared below)
    return 301 https://www.example.com$request_uri;
}

# Main https://www. server block
server {
    listen [::]:443 ssl http2;
    listen 443 ssl http2;

    # listen on the wrong host
    server_name example.com;

    # Paths to SSL certificate files.
    ssl_certificate         /etc/ssl/ssl-example.com.crt;
    ssl_certificate_key     /etc/ssl/ssl-example.com.key;
    ssl_trusted_certificate /etc/ssl/ssl-example.com.crt;

    ssl_dhparam         /etc/ssl/ssl-example.com.pem;

    # Include defaults for allowed SSL/TLS protocols and handshake caches.
    include /etc/nginx/custom/ssl.conf;

    root /data/sites/example.com/;
    index index.html index.phtml;

}

你能发布准确的错误消息吗?任何重定向路径都有效吗?(非www-http->www-https,www-http->www-https,非www-https->www-https)@AnandBhat它似乎只是在没有www的情况下重定向到。然后它说重定向失败了。对于不同的浏览器,消息是不同的。对于firefox,它说:“页面没有正确重定向”。对于chrome,它说:“example.com重定向你太多次了。”你能发布准确的错误消息吗?任何重定向路径都有效吗?(非www-http->www-https,www-http->www-https,非www-https->www-https)@AnandBhat它似乎只是在没有www的情况下重定向到。然后它说重定向失败了。对于不同的浏览器,消息是不同的。对于firefox,它说:“页面没有正确重定向”。对于chrome,它说:“example.com重定向你太多次了。”错误日志怎么说?我在生产中的许多服务器上都有这样的工作…错误太多了,重定向太多了,我得到了这个错误日志上说了什么?我在生产中的许多服务器上都有这个功能…呃,太多重定向了,我得到了这个