我将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
}
我解决了我的问题,并将列出解决该问题的步骤,以供将来其他人参考:
我最初尝试的Nginx配置是正确的(如问题中所述)。
CNAME
记录添加到DNS条目这是主要的丢失部分。我CNAME
在DNS仪表板中添加了一个条目,如下所示:
类型: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
现在一切都很好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句