nginx enforce https适用于www.example.com但不适用于example.com

时间:2014-08-05 15:34:22

标签: redirect nginx playframework-2.1

我已经在ubuntu服务器12.04 lts中通过nginx实施了https强制执行。 nginx版本:nginx / 1.1.19。我在路径/etc/nginx/conf.d/https.conf中添加了一个conf文件,并将其包含在nginx.conf中。它看起来像:

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

配置的其余部分只是默认值:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
   # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

通过www.example.com访问时,它正确地将我重定向到网络的https版本。

相反,当通过 example.com 进行访问时,只有当应该重定向到https://www.example.com时才会通过 http 版本。

配置中有什么问题吗?应用程序服务器正在侦听 9000 9443 端口(分别为http和https),并由iptables通过以下规则转发:

-A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 9000
-A PREROUTING -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 9443

提前致谢

1 个答案:

答案 0 :(得分:0)

问题在于iptables正在使用以下规则绕过nginx:

 -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 9000

我以前用过nginx安装。实际上,nginx是一个服务器在某个端口侦听,而iptables在内核级别工作。

当删除此规则时,所有流量:80都通过nginx,无论是www.mysite.com,mysite.com是否带有http前缀或我想要的任何组合。