我们已经有一个运行良好的网站,但我被要求将另一个网址指向同一个网站。这个想法是地址栏中会显示两个网址,但只会显示一个网站。
我在 DNS 管理站点添加了一条 A 记录,将新域名指向旧站点所在服务器的 IP 地址,并收到传统页面,内容如下
“欢迎使用 nginx!如果您看到此页面,...”
因此,在研究如何配置我的服务器时,我遇到了本网站这看起来很有希望并且使任务看起来很简单。
所以我所做的就是server { ... }
在我的 中复制我的/etc/nginx/sites-available/beta.conf
,并在我的 中复制我的 ,/etc/nginx/sites-enabled/beta.conf
就像这样(请注意,块之间的唯一区别server
是server_name
)。
nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# 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/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
}
default:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}
upstream beta_app_server {
server unix:/home/beta/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name beta.portal.barefootretirement.com;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/beta.portal.barefootretirement.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/beta.portal.barefootretirement.com/privkey.pem;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
client_max_body_size 4G;
access_log /home/beta/logs/nginx-access.log;
error_log /home/beta/logs/nginx-error.log;
location /static/ {
alias /home/beta/static/;
}
location /media/ {
alias /home/beta/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://beta_app_server;
break;
}
}
}
server {
listen 80;
server_name beta.gowealthpoint.com;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/beta.portal.barefootretirement.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/beta.portal.barefootretirement.com/privkey.pem;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
client_max_body_size 4G;
access_log /home/beta/logs/nginx-access.log;
error_log /home/beta/logs/nginx-error.log;
location /static/ {
alias /home/beta/static/;
}
location /media/ {
alias /home/beta/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://beta_app_server;
break;
}
}
}
我去测试一下这个配置是否适用于
sudo nginx -t
但我收到了这些警告
nginx: [warn] conflicting server name "beta.gowealthpoint.com" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "beta.gowealthpoint.com" on 0.0.0.0:443, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
当我尝试重启服务器时
service nginx restart
它失败。
这两个位置都没有留下任何临时文件。我使用
ls -lah
很明显我做错了什么,但我不知道是什么。任何帮助都将不胜感激。
答案1
如果nginx -t
运行正确,但 nginx 重启失败,则可能有一个 nginx 进程在没有正确的 PID 文件的情况下运行。
这意味着停止 nginx 进程会失败,当系统尝试启动它时,会失败,因为它已经在运行。在这种情况下,nginx 仍然使用旧的配置。
尝试service nginx stop
,使用命令检查 nginx 是否正在运行ps
。如果它仍在运行,请使用kill
停止它,然后尝试service nginx start
。
答案2
我认为可能更有意义:
- 为你的 HTTP 站点设置一个最小的服务器块,它(很可能)仅有的重定向到 HTTPS
- 并为您的 HTTPS 网站设置一个服务器块
此外,您可能需要考虑是否要拥有一个 HTTPS 站点,还是两个(即每个域一个) - 换句话说,您可以让两个 HTTP 服务器块看起来像这样:
server {
listen 80;
server_name beta.barefootretirement.com beta.gowealthretirement;
return 301 https://beta.barefootretirement.com$request_uri;
}
或者您可以创建一个类似的块来处理两个域的 HTTPS 请求(取决于您的需要)。
我不完全清楚究竟是什么导致了你的问题(现在无法深入研究),但我认为减少配置中的冗余肯定不会有坏处。
所以我所做的就是在我的 /etc/nginx/sites-available/beta.conf 和我的 /etc/nginx/sites-enabled/beta.conf 中复制我的服务器 {...}
关于此评论的一个小事:通常,您编辑 sites-available 文件,并通过将该文件符号链接到 /etc/nginx/sites-enabled 来启用该站点 - 我感觉您目前有两个文件。
这可能与您的问题无关,但在这里,删除不必要的东西也不会有什么坏处。
答案3
为什么不这样做呢?
server {
server_name beta.portal.barefootretirement.com beta.gowealthpoint.com;
listen 80;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/beta.portal.barefootretirement.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/beta.portal.barefootretirement.com/privkey.pem;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
client_max_body_size 4G;
access_log /home/beta/logs/nginx-access.log;
error_log /home/beta/logs/nginx-error.log;
location /static/ {
alias /home/beta/static/;
}
location /media/ {
alias /home/beta/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://beta_app_server;
break;
}
}
}
您所需要的只是一个服务器指令,并且两个服务器名称都在一行上...此外,出于性能原因,通常不建议使用 if 语句,最好这样做:
server {
server_name beta.portal.barefootretirement.com beta.gowealthpoint.com;
listen 80;
listen [::]:80;
return 301 https://$server_name$request_uri;
}
server {
server_name beta.portal.barefootretirement.com beta.gowealthpoint.com;
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/letsencrypt/live/beta.portal.barefootretirement.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/beta.portal.barefootretirement.com/privkey.pem;
client_max_body_size 4G;
access_log /home/beta/logs/nginx-access.log;
error_log /home/beta/logs/nginx-error.log;
location /static/ {
alias /home/beta/static/;
}
location /media/ {
alias /home/beta/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://beta_app_server;
break;
}
}
}