nginx强制SSL无www重定向问题

时间:2015-10-05 01:15:07

标签: linux redirect ssl nginx

过去几天我一直试图让这个工作正常,但是我的时间很短。

(stackoverflow不允许我发布两个以上的实际链接 - 忽略反斜杠)

通常的结果是www.example\.nethttp\s://www.example\.net会重定向到http \ s://example.net,而 example\.net => http\s://example\.net www.example\.net => http\s://example\.net https://www.example\.net => http\s://example\.net 则不会重定向到http \ s://示例。 net,另一个常见情况是来自浏览器的消息声称页面没有正确重定向。

我试图做的是强制所有请求转到http \ s://example.net$request_uri。

upstream backend {
  server unix:/var/run/php5-fpm.sock;
}

server {
  listen 80;
  server_name www.example\.net example.net;
  return 301 http\s://example\.net$request_uri;
}

server {
  listen 443 ssl default_server;

  server_name example\.net;

  include snippets/snakeoil.conf;

  root /var/www/html/example;

  charset utf-8;
  index index.php;

  access_log /var/log/example-access.log;
  error_log /var/log/example-error.log;

  location / {
    try_files $uri $uri/ @extensionless-php;
    index index.php;
  }

  location ~ .php {
    try_files $uri =404;
    fastcgi_pass backend;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

  location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
  }

  location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
  }
}

nginx网站配置:

public class Encode {

    /**
     * Main method - this does not need to be changed at all.
     */
    public static void main(String[] args) {
        testing();
        userInteraction();
    }

    /**
     * This method is used to test the encrypt method.
     */
    public static void testing() {

        String testMessage1 = "test";
        int testKey1 = 11;
        String result = encrypt(testMessage1, testKey1);
        System.out.println("Encrypted result: "+result);
    }

    /**
     * This method changes each character in a String to a
     * different character based on the key passed in as
     * an integer. The new String created by the encryption
     * process is returned.
     *
     * @param message   the String to be encoded
     * @param key       the integer value used for encryption
     * @return          a new encoded String
     */
    public static String encrypt(String message, int key) {
        System.out.println("encoding: "+message+", with key: "+key);
        String encodedMessage = "";
        message=message.toUpperCase();

        int length = message.length();
        for(int i=0;i<length;i++)
        {
            int characterValue = (int)message.charAt(i);
            characterValue = characterValue + key;
                if(characterValue>90)
                {
                    characterValue = (65 + key ); // <---- What needs to go here? In order to make value loop back to A.
                }

            char myChar = (char)characterValue;
            encodedMessage=encodedMessage+myChar;
        }


        return encodedMessage;
    }

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

创建2个块来处理重定向到非www HTTPS。 最后一个区块是您放置常用网站的配置/规则的地方。

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

server {
    listen               443 ssl;
    server_name          www.example.net;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;
    return               301 https://example.net$request_uri;
}
server {
    listen               443 ssl;
    server_name          example.net;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;

    # the rest goes here...
}