大网数据广告活动
服务器运维问答

NGINX redirect for multiple domains

· 52CE 我爱测

I'm very new to NGINX and the whole configuration, so I managed to setup a server with the following config with the help from a friend: server { listen 80; listen [::]:80; server_name domain.com; rewrite ^ https://$server_name$r…

问题原文

原帖链接 · CentrixDE · CC BY-SA 3.0

I'm very new to NGINX and the whole configuration, so I managed to setup a server with the following config with the help from a friend:

    server {
        listen 80;
        listen [::]:80;
        server_name domain.com;
        rewrite         ^       https://$server_name$request_uri? permanent;
}

server {
        listen 80;
        listen [::]:80;
        server_name domain.de;
        rewrite         ^       https://$server_name$request_uri? permanent;
}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name domain.com domain.de;
        root /var/www/html;
        index index.php index.html index.htm;

there are multiple servers for my main page, some project pages, and some private pages. I have for every single TLD an own server because I don't want to have the TLD in the address bar changed on a redirect. Now my answer is, how can I simply get all these non-SSL Servers to one big Server(1/project) without redirecting to just one TLD?

Example:

Project.com, Project.de, Project.net and Project.org are redirecting over 5 of these non-SSL Servers to one SSL-enabled Server. When i'm going to http://project.net i'm beeing redirected to https://project.net. The TLD doesnt change at all, because of the single non-SSL Server setup.

What i want to achieve, that i can minify my config file to have one non-SSL Server per Project redirecting to https without changing the domain.

                                       :80 Server      :443 Server

http domain.net ---> https domain.net ([non-SSL 01] ---> [SSL 01]) http domain.com ---> https domain.com ([non-SSL 02] ---> [SSL 01]) http domain.de ---> https domain.de ([non-SSL 03] ---> [SSL 01])

http project.de ---> https project.de ([non-SSL 04] ---> [SSL 02]) http project.com ---> https project.com ([non-SSL 05] ---> [SSL 02]) and so on

已采纳解答

原帖链接 · jarvis · CC BY-SA 3.0

What I understand is that you want to redirect multiple domains to one domain.

First, I suggest you use return 301 instead of rewrite. It is more efficient in this case.

This is the default server config. It will be served if the request does not match any of the virtual host.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://your-correct-domain.com$request_uri;
}

Also setup the a server to receive HTTP traffic for your-correct-domain.com and redirect it to HTTPS.

server {
    listen 80;
    listen [::]:80;
    server_name your-correct-domain.com;
    return 301 https://your-correct-domain.com$request_uri;
}

Then finally, the HTTPS server of your-correct-domain.com where you have all your config.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name your-correct-domain.com;
    root /var/www/html;
    index index.php index.html index.htm;
}

Note that this setup will serve the default server if someone accesses the IP directly. For example, your server IP is 192.168.100.100, if someone enters that in their browser, they will be redirected to https://your-correct-domain.com.

The default server will catch all request that does not have a server/virtual host prepared for it. So if someone requests for domain.de, my.domain.de, site2.domain-something.com, they will all be permanently redirected to https://your-correct-domain.com. You also have the option to redirect with 302 instead of 301.

Final note, if you have other virtual host that is not part of this setup you are making, it will not be a problem. The default server will not be served for that because Nginx will check first if a virtual host exist and serve that first.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。