我将Nginx用作Apache的反向代理。我有一个域,假设example.com
指向/var/www/html/example
目录。
我想将子域phpmyadmin.example.com
指向目录/var/www/html/phpmyadmin
,该目录显然不是该域目录的子目录。
我的完整配置文件/etc/nginx/sites-enabled/example
如下所示。我将##### BEGIN ..
和###### END ..
之间的部分添加到我的配置文件中,以尝试完成此工作。
然后我做了sudo service nginx restart
成功地重新启动了NGINX,但是我仍然无法访问phpmyadmin.example.com
。
这是我的配置:
server {
root /var/www/html/example;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com;
access_log /var/www/html/example/access.log;
error_log /var/www/html/example/error.log;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.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
}
##### BEGIN PHPMYADMIN CONFIG #####
server {
root /var/www/html/phpmyadmin;
index index.php index.html index.htm index.nginx-debian.html;
server_name phpmyadmin.example.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
###### END OF PHPMYADMIN CONFIG #####
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name example.com www.example.com
return 404; # managed by Certbot
}
答案 0 :(得分:1)
我解决了我的问题,我将列出为解决该问题而采取的步骤,以供将来其他人参考:
我最初尝试的nginx配置是正确的(如问题中所述)。
CNAME
记录添加到DNS条目这是主要的丢失部分。我在DNS仪表板中添加了一个CNAME
条目,如下所示:
类型:CNAME
名称:phpmyadmin
值:example.com
TTL:3600
(也可以设置为自动)
我将certbot
用于SSL证书,我不得不重新生成它以将域包括在证书中。
sudo certbot --nginx -d example.com -d www.example.com -d phpmyadmin.example.com
在此步骤中,certbot可以处理nginx配置,以将HTTP流量重定向到HTTPS。
重新加载了新配置:sudo service nginx reload
并验证了服务状态:sudo service nginx status
现在一切正常。