Redirect nginx enforce https适用于www.example.com,但不适用于example.com
我已经在ubuntu服务器12.04 lts中通过nginx实现了https。nginx版本:nginx/1.1.19。我在路径/etc/nginx/conf.d/https.conf中添加了一个conf文件,并将其包含在nginx.conf中。它看起来像:Redirect nginx enforce https适用于www.example.com,但不适用于example.com,redirect,nginx,playframework-2.1,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_ur
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访问时,它会正确地将我重定向到web的https版本
相反,当通过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
提前感谢问题在于,使用以下规则的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前缀)还是我想要的任何组合 请在那里发布完整的nginx config.here,它们是默认设置。如果您使用
site.com
到达那里,它能工作吗?这就是你的server\u name
lineserver\u name site.com www.mysite.com代码>。抱歉,这是一个输入错误:当通过www.mysite.com访问时,它会正确地将我重定向到web的https版本。相反,当通过mysite.com进行访问时,它只会在应该重定向到的时候通过http版本。