我的服务器上运行着多个服务,这些服务将通过 nginx 访问并由 certbot 加密。如果我想使用 访问我的服务http://example.com
,我会被重定向到http(s)://example.com
,这很棒。
但是,如果我输入我的,IpAdress:Port
则不会被重定向到我的域名。这是我的 abc.com 文件/etc/nginx/sites-enabled
server {
server_name abc.com; #example: mysite.xyz
#access_log /var/log/nginx/<servicename>.access.log;
#error_log /var/log/nginx/<servicename>.error.log;
location / {
proxy_pass http://127.0.0.1:9000; # here you define the address, which is used by nginx to access your service
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
} # this is the port you use to access the proxied service
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/abc.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/abc.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = abc.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name abc.com;
listen 80;
return 404; # managed by Certbot
}
有人能告诉我或指出我需要在 abc.com 文件中进行哪些更改才能将请求重定向IpAdress:Port
到https://example.com
我很感激任何帮助!
编辑:我已使我的服务可通过 localhost 访问,这解决了我的问题。感谢大家的贡献!
答案1
改变最后一行
listen 80 default;
答案2
欢迎来到 ServerFault。
有人能告诉我或指出我需要在 abc.com 文件中进行哪些更改才能将通过 IpAdress:Port 的请求重定向到https://example.com
您不必编辑 Certbot 将来可能会修改的现有文件。
相反,/etc/nginx/sites-enabled
请在 中创建另一个文件(例如,名为ip.conf
),并包含以下内容...
server {
listen 80;
server_name 127.0.0.1;
return 301 https://example.com$request_uri;
}
在上面的代码中,请将其替换127.0.0.1
为您服务器的实际IP地址,然后example.com
也进行替换。