将 Apache2 配置转换为 nginx 配置

将 Apache2 配置转换为 nginx 配置

我已将 RHEL 6 与 apache/httpd (v2.2) 配置为处理 WordPress 的两个不同路径:

FOO 路径:/var/www/html/FOO/

BAR路径:/var/www/html/BAR/

我们计划迁移到一台运行 RHEL 7 和 nginx 的新机器,我需要帮助/协助将旧的 PHP 配置重写为新的 nginx 配置

这是我的旧 Apache2 配置

<VirtualHost *:80>
    DocumentRoot /var/www/html/FOO
    ServerName www.Example.com
    ServerAlias Example.com
    Serveralias web.Example.com
    Alias /en/ /var/www/html/BAR/
    <Directory /var/www/html/BAR>
        Options FollowSymLinks -Indexes
        AllowOverride all
    </Directory>
</VirtualHost>
<VirtualHost *:443>
    DocumentRoot /var/www/html/FOO
    ServerName web.Example.com

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.Example\.com$
    RewriteRule ^(.*)$ "https://www.Example.com$1" [L]
</VirtualHost>
<VirtualHost *:443>
    DocumentRoot /var/www/html/FOO
    ServerName Example.com


    RewriteEngine On
    RewriteRule ^(.*)$ "https://www.Example.com/$1" [L]

</VirtualHost>
<VirtualHost *:443>
    DocumentRoot /var/www/html/FOO
    ServerName www.Example.com
    <IfModule mod_info.c>
        <Location /server-info>
                SetHandler server-info
                Order deny,allow
                Deny from all
                Allow from 127.0.0.1
        </Location>
    </IfModule>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.Example\.com\.* [NC]
    RewriteRule ^(.*)$ https://www.Example.com$1 [L]
    Alias /en/ /var/www/html/BAR/
    <Directory /var/www/html/BAR>
</VirtualHost>

我的尝试 NGINX 配置代码

server {
listen 80 ;
listen [::]:80 ;
server_name Example.com www.Example.com;
return 301 https://$host$request_uri;
    }

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

ssl_certificate /opt/digicert/bundle.pem;
ssl_certificate_key /opt/digicert/key-nopass.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m;
ssl_session_tickets off;

ssl_dhparam /opt/digicert/dhparam.pem;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;

add_header Strict-Transport-Security "max-age=63072000" always;

ssl_stapling on;
ssl_stapling_verify on;

ssl_trusted_certificate /opt/digicert/DigiCertCA.pem;

resolver 8.8.8.8 valid=10s;

root /var/www/html/FOO;
index index.php index.html index.htm;

location / {
            try_files $uri $uri/ /index.php?$args;
    }

location /BAR/ {
            root /var/www/html/BAR/;
            try_files $uri $uri/ /index.php?$args;
    }

location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
    }
    
    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

location ~ \.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    }

答案1

既然您说将/var/www/html/BAR目录内容移动到/var/www/html/FOO/en目录中是您可以接受的,那么就这样做并使用以下配置:

server {
    ...
    root /var/www/html/FOO;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location /en/ {
        try_files $uri $uri/ /en/index.php?$args;
    }

    ...

您可以保持其他位置不变。

这样,任何以 开头的请求/en/都会转到第二个 WordPress 实例,而所有其他请求都会转到第一个 WordPress 实例。

相关内容