我有一个与Nginx重定向有关的问题 在下面您可以看到配置。 我的目标是从https://example.com重定向到https://www.example.com
我检查了stackoverflow中的几乎所有内容,但没有找到任何帮助。请帮我解决这个问题。我将提供有关Nginx Web服务器的所有必要信息。 希望您能向我提出这个难题。
我的文件nginx.conf
如下所示:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_static on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 9;
# gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xm$
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
我的文件/etc/nginx/sites-enabled/example:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_stapling on;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
root /var/www/example/public;
index ../views/index.html;
location /img/ {
proxy_pass http://127.0.0.1:3010;
proxy_cache off;
proxy_cache_key "$proxy_host$uri$is_args$args";
}
location / {
proxy_pass http://127.0.0.1:3010;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|css|js|html)$ {
root /var/www/example/public;
expires 1y;
access_log off;
add_header Cache-Control "public";
}
}
答案 0 :(得分:1)
有关重定向以及ssl的快速说明
不要将所有站点的所有配置都写在一个文件nginx.conf
中。分开这些。您有两个文件夹/etc/nginx/sites-available/
和/etc/nginx/sites-enabled/
为您的网站添加文件,例如/etc/nginx/sites-available/example
建立链接ln -s /etc/nginx/sites-enabled/example
要在此conf文件中粘贴以下文本:
server {
listen 80;
server_name example.com www.cova.company;
return 301 https://www.example.company$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_stapling on;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.site.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.site.com/privkey.pem;
# your location there a
}
在您nginx.conf
中,ypu已经拥有行include /etc/nginx/sites-enabled/*;
,这意味着自动从文件夹sites-enabled
中获取其所有站点配置
使用命令nginx -t
检查语法并使用命令systemctl reload nginx
重新加载nginx
毕竟,通过http://example.com或https://example.com呼叫您网站的用户将被重定向到https://www.example.com
答案 1 :(得分:0)
只需为非www请求创建服务器,例如:
# redirect http to https
server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.example.com$request_uri;
}
# redirect http://example.com to https://www.example.com
server {
listen 443 ssl;
server_name example.com;
# ssl ...
return 301 https://www.example.com$request_uri;
}
# https://www.example.com
server {
listen 443 ssl;
server_name www.example.com;
# ssl ...
}
example.com
和www.example.com
的DNS记录应指向您的Nginx服务器