配置Nginx反代WordPress,并为WordPress添加多域名

默认情况下wordpress只支持绑定单域名,通过Nginx反向代理实现多域名映射。

原理简述:nginx代理php-fpm到127.0.0.1:79,再对127.0.0.1:79反向代理,将通过其他域名访问的流量间接转发到php-fpm。HTTPS需在反代步骤添加。

Nginx配置:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 1024;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 60;
    types_hash_max_size 2048;

    client_max_body_size 20m;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_session_timeout 5m;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE TLSv1 
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    
    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;

    # 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/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    server {
        listen 127.0.0.1:79;
        server_name default;

        # 网站目录,主页
        root /var/www/c-o.ltd;
        index index.php;

        # 伪静态
        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        # 伪静态
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        # php-fpm
        location ~ \.php$ {
            fastcgi_pass   unix:/run/php/php7.3-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    server {
        listen 80;
        server_name c-o.ltd www.c-o.ltd;
        rewrite ^(.*)$ https://www.c-o.ltd permanent;
    }

    # ipv4反向代理,内网443,外网4443
    server {
        ssl on;
        listen 443;
        listen 4443;
        server_name c-o.ltd www.c-o.ltd;

        # ssl配置
        ssl_certificate ssl/c-o.ltd.crt;
        ssl_certificate_key ssl/c-o.ltd.key;

        location / {
            proxy_pass http://localhost:79/;

            #Proxy Settings
            proxy_redirect     off;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }

    # ipv6反向代理,外网80,跳转HTTPS
    server {
        listen [::]:80;
        server_name ipv6.c-o.ltd;
        rewrite ^(.*)$ https://www.kkklink.com permanent;
    }

    # ipv6反向代理,外网443
    server {
        ssl on;
        listen [::]:443;
        server_name ipv6.c-o.ltd;
        # ssl配置
        ssl_certificate ssl/c-o.ltd.crt;
        ssl_certificate_key ssl/c-o.ltd.key;

        location / {
            proxy_pass         http://localhost:79/;

            #Proxy Settings
            proxy_redirect     off;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }
}

wordpress配置(添加在wp-config.php):

if((!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) || (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) {
    $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
    $_SERVER['HTTPS'] = 'on';
}
define('WP_HOME', 'https://'.$_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'https://'.$_SERVER['HTTP_HOST']);

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇