gpt4 book ai didi

wordpress - 使用 nginx 将子域映射到 URL

转载 作者:行者123 更新时间:2023-12-01 01:12:20 30 4
gpt4 key购买 nike

我对 nginx 很陌生,所以如果我的解释不正确,请原谅我。我会尽力解释我想要达到的目标。

使用 WordPress 和 nginx,我希望将用户帐户映射到主域的子域。例如,如果用户创建一个名为“sample”的帐户,则该用户的子域将为 sample.example.com .

当用户转到 sample.example.com ,子域应该映射到 example.com/sample/ .同样,如果用户访问 sample.example.com/account/ ,它应该映射到 example.com/sample/account/ , 等等等等。需要注意的是example.com/sample/ URL 是这种结构的重写:example.com/index.php?user=sample .

还有一些保留的子域不应重定向 ,例如cdn和admin。如果需要,这些规则应该忽略它们。

当用户创建帐户时,如何自动实现这一点?这里的目标是自动化 - 正确设置一次,不用担心。由于我几天前刚刚开始使用 nginx,我完全不知道从哪里开始。任何让我朝着正确方向前进的建议都会非常有帮助。这是我当前的域配置文件:

server {
listen 80;
server_name www.example.com;
rewrite ^(.*) $scheme://example.com$1 permanent;
}

server {
listen 443 ssl;
server_name www.example.com;
rewrite ^(.*) $scheme://example.com$1 permanent;
}

server {
listen 80;
server_name example.com;

access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;

root /var/www/example.com/public;
index index.php;

location / {
try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
}

location @wordpress {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

# Pass the PHP scripts to FastCGI server listening on UNIX sockets.
#
location ~ \.php$ {
try_files $uri @wordpress;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public$fastcgi_script_name;
include fastcgi_params;
}
}

server {
listen 443 ssl;
ssl on;
keepalive_timeout 70;
server_name example.com;
ssl_certificate ssl/example.com.chained.crt;
ssl_certificate_key ssl/example.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;

root /var/www/example.com/public;
index index.php;

location / {
try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
}

location @wordpress {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

# Pass the PHP scripts to FastCGI server listening on UNIX sockets.
#
location ~ \.php$ {
try_files $uri @wordpress;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public$fastcgi_script_name;
include fastcgi_params;
}
}

我知道我想要实现的目标可能需要进入 /etc/nginx/nginx.conf如果我希望它自动化,我正在积极尝试学习如何实现这一目标。我只是被困在我现在的位置,正在寻找任何可以为我指明正确方向的建议/帮助。我渴望学习!

最佳答案

答案

经过几天的搜索、调整和配置,我已经编写了将子域映射到 URL 所需的代码,就像我的示例一样。这是我的 example.com 虚拟主机:https://gist.github.com/thomasgriffin/4733283

server {
listen 80;
listen 443 ssl;
server_name ~^(?<user>[a-zA-Z0-9-]+)\.example\.com$;

location / {
resolver 8.8.8.8;
rewrite ^([^.]*[^/])$ $1/ permanent;
proxy_pass_header Set-Cookie;
proxy_pass $scheme://example.com/user/$user$request_uri;
}
}

server {
listen 80;
listen 443 ssl;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}

server {
listen 80;
server_name example.com;

access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;

root /var/www/example.com/public;
index index.php;

location / {
try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
}

location @wordpress {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

# Pass the PHP scripts to FastCGI server listening on UNIX sockets.
#
location ~ \.php$ {
try_files $uri @wordpress;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

server {
listen 443 ssl;
ssl on;
keepalive_timeout 70;
server_name example.com;
ssl_certificate ssl/example.com.chained.crt;
ssl_certificate_key ssl/example.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_prefer_server_ciphers on;

root /var/www/example.com/public;
index index.php;

location / {
try_files $uri $uri/ @wordpress /index.php?q=$request_uri;
}

location @wordpress {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

# Pass the PHP scripts to FastCGI server listening on UNIX sockets.
#
location ~ \.php$ {
try_files $uri @wordpress;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

映射的主要块在第一个服务器块中完成。我的目标是任何子域(我已经用其他不相关的代码清除了受限制的子域)并重写它以确保它具有尾部斜杠,以避免 WordPress 对没有尾部斜杠的 URL 进行任何内部重定向。从那里, resolver指令需要解析 proxy_pass 中定义的 URL。 ,所以我正在使用 Google 的 DNS 进行解析。我也在使用 proxy_pass_header指令发送 cookie 以保持 WordPress 登录身份验证。 proxy_pass定义要映射到的 URL。

还应该注意的是,如果您想对子域也使用登录身份验证,则需要在 wp-config.php 中定义您的自定义 cookie 域。像这样:
define('COOKIE_DOMAIN', '.example.com');

应该就是这样。您现在可以享受像 subdomain.example.com 这样的 URL。那个映射到 example.com/user/subdomain/或任何你想要的。从那里,您可以利用 WordPress 的重写 API 将映射的 URL 映射到可以发送到 $wp_query 的特定查询参数。用于加载自定义模板等。

关于wordpress - 使用 nginx 将子域映射到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14711666/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com