nginx将非www重定向到https上的www

时间:2016-05-02 12:21:43

标签: redirect nginx

这是我当前的服务器块:

server {
    listen 80;
    server_name www.domain.com domain.com;
    return 301 https://www.domain.com$request_uri;
    access_log /var/log/nginx/domain.com.access;
    error_log /var/log/nginx/domain.com.error error;
}


## BETA
server {
    listen              443 ssl http2;
    keepalive_timeout   300;

这非常有效地将非https www和非https非www重定向到https://www.domain.com

不幸的是,https非www没有被重定向,所以我改为:

server {
    listen 80;
    server_name www.domain.com domain.com;
    return 301 https://www.domain.com$request_uri;
    access_log /var/log/nginx/domain.com.access;
    error_log /var/log/nginx/domain.com.error error;
}


## BETA
server {
    listen              443 ssl http2;
    server_name domain.com;
    return 301 https://www.domain.com$request_uri;
    keepalive_timeout   300;

不幸的是,这只会导致服务器崩溃。

对于我做错的任何意见都将不胜感激。

这里要求的是我的conf文件。 nginx -t返回成功

server {
    listen 80;
    server_name www.example.com example.com;
    return 301 https://www.example.com$request_uri;
    access_log /var/log/nginx/example.com.access;
    error_log /var/log/nginx/example.com.error error;
}


## BETA
server {
    listen              443 ssl http2;
    keepalive_timeout   300;
    charset utf-8;

    add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains; preload';

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    ssl_certificate     /etc/ssl/certs/wc.example.com.pem;
    ssl_certificate_key /etc/ssl/certs/wc.example.com.key;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 30m;

    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    server_name  www.example.com;

    # SSL buffer size was added in 1.5.9
    ssl_buffer_size      1400; # 1400 bytes to fit in one MTU

    access_log /var/log/nginx/dev.example.com.access;
    error_log /var/log/nginx/dev.example.com.error error;

    root   /srv/storage/pxpubl29/www.example.com/htdocs/;
    index index.php index.html index.htm;

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

    location ~ ^/pages {
    try_files $uri /index.php$is_args$args;
    rewrite ^/pages/free_chat.php$ /pages/free-chat.php permanent;
    }

    location ~ ^/upl {

    client_max_body_size 400M;
    client_body_buffer_size 1024k;

    # For CORS
    if ($request_method = OPTIONS) {
        add_header Pragma no-cache;
        add_header X-Content-Type-Options nosniff;

        # Access control for CORS
        add_header Access-Control-Allow-Origin "https://www.example.com";
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
        add_header Access-Control-Allow-Headers "cache-control, content-range, accept, origin, session-id, content-disposition, x-requested-with, content-type, content-description, referer, user-agent";
        add_header Access-Control-Allow-Credentials "true";

        # 10 minute pre-flight approval
        add_header Access-Control-Max-Age 600;

        return 204;
    }

    if ($request_method = POST) {
        add_header Pragma no-cache;
        add_header X-Content-Type-Options nosniff;
        #add_header Cache-control "no-story, no-cache, must-revalidate";

        # Access control for CORS
        add_header Access-Control-Allow-Origin "https://www.example.com";
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
        add_header Access-Control-Allow-Headers "cache-control, content-range, accept, origin, session-id, content-disposition, x-requested-with, content-type, content-description, referer, user-agent";
        add_header Access-Control-Allow-Credentials "true";

        # 10 minute pre-flight approval
        add_header Access-Control-Max-Age 600;
    }

        location ~ \.php$ {

            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            fastcgi_param PHP_VALUE "session.name=space_sid
        session.cookie_domain=.example.com
        upload_max_filesize=400M
        post_max_size=400M";

            include fastcgi_params;
            fastcgi_index index.php;
    }

    }

    location ~ \.php$ {

        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_param PHP_VALUE "session.name=space_sid
    session.cookie_domain=.example.com
    upload_max_filesize=400M
    post_max_size=400M";

        include fastcgi_params;
        fastcgi_index index.php;
    }

    location /samples {
    return 301 /;
    }

    location ~ ^/large {
    try_files $uri $uri/ /large/index.php;
    location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_index index.php;
    }
    }

    location /avc {
    rewrite ^/avc/users_(.+)$ /avc/users_php.php break;
    rewrite ^/avc/undefined$ /avc/integration_xml.php break;
    rewrite ^/avc/avc_settings.xml$ /avc/integration_xml.php break;

        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_index index.php;
    }

    location = /avc/avc_settings.xml {
        rewrite ^(.*)$ /avc/integration_xml.php break;

        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_index index.php;
    }

    location /upl/server/php/files {
        location ~ \.php$ {return 403;}
    }

    location ~ .*/\.git {
        deny all;
    }

    # Force the latest IE version
    add_header "X-UA-Compatible" "IE=Edge";

    # This tells Nginx to cache open file handles, "not found" errors, metadata about files and their permissions, etc.
    open_file_cache          max=1000 inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;

    #location ~* \.(js|css|png|jpg|jpeg|gif|ico|ttf|svg)$ {
    #    expires 14d;
    #    add_header Pragma "public";
    #    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    #}

# The X-Frame-Options header indicates whether a browser should be allowed
# to render a page within a frame or iframe.
add_header X-Frame-Options SAMEORIGIN;

# MIME type sniffing security protection
#   There are very few edge cases where you wouldn't want this enabled.
add_header X-Content-Type-Options nosniff;

# The X-XSS-Protection header is used by Internet Explorer version 8+
# The header instructs IE to enable its inbuilt anti-cross-site scripting filter.
add_header X-XSS-Protection "1; mode=block";

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
  expires -1;
}

# Feed
location ~* \.(?:rss|atom)$ {
  expires 1h;
  add_header Cache-Control "public";
}

# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|swf|mp3)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
  expires 1y;
  access_log off;
  add_header Cache-Control "public";
}

# WebFonts
# If you are NOT using cross-domain-fonts.conf, uncomment the following directive
location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ {
 expires 1M;
 access_log off;
 add_header Cache-Control "public";
}



    # ERROR Pages
    error_page   404          /error/404/;
    error_page   403          /error/403/;
    error_page   401          /error/401/;

    # ACL
    # deny all;
}

0 个答案:

没有答案
相关问题