如何在同一服务器中处理从nginx到apache的多个主机名句柄?

时间:2018-11-06 00:20:14

标签: apache nginx vhosts

我计划在同一台服务器上管理多个网站,目前我正在处理nginx的http请求,然后将其处理到apache。

这是我当前第一个网站的配置:

  # Force HTTP requests to HTTPS
server {
listen 80;
server_name myfirstwebsite.net;
return 301 https://myfirstwebsite.ne$request_uri;
}

  server {
  listen  443 ssl;
  root  /var/opt/httpd/ifdocs;

server_name myfirstwebsite.ne ;

  # add Strict-Transport-Security to prevent man in the middle attacks
add_header Strict-Transport-Security "max-age=31536000" always;
ssl on;
ssl_certificate     /etc/pki/tls/certs/cert.pem;
ssl_certificate_key /etc/pki/tls/certs/cert.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers         HIGH:!aNULL:!MD5;

access_log /var/log/nginx/iflogs/http/access.log;
error_log  /var/log/nginx/iflogs/http/error.log;


###include rewrites/default.conf;
index  index.php index.html index.htm;

# Make nginx serve static files instead of Apache
# NOTE this will cause issues with bandwidth accounting as files wont be logged
location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ {
    expires max;
}

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
       proxy_pass https://127.0.0.1:4433;
}

# proxy the PHP scripts to Apache listening on <serverIP>:8080
location ~ \.php$ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
      proxy_pass https://127.0.0.1:4433;
}

location ~ /\. {
    deny  all;
}

error_page  500 502 503 504  /50x.html;
location = /50x.html {
    root  /usr/share/nginx/html;
}
}

现在,我的问题是,对于第二个,第三个网站,依此类推,我正在考虑修改该行:

proxy_pass https://127.0.0.1:4433;

对于

proxy_pass https://secondwebsite.net:4433;

但是我不想做的是,离开互联网,查找该dns,然后返回同一台服务器,但在同一台服务器中使用(这就是为什么我有localhost:4433的原因在第一个网站中),因此不会出现延迟问题。 有什么解决办法吗?

此外,我想知道如果使用同一端口(在本例中为4433)为多台服务器提供服务还是每个网站都必须使用不同的端口是否会出现问题。

先谢谢您。

1 个答案:

答案 0 :(得分:1)

多个服务器配置

一种方法是拥有多个服务器块,最好是在不同的conf文件上。这样的操作会对新文件(例如/etc/nginx/sites-available/mysecondwebsite)中的第二台服务器起作用:

 # Force HTTP requests to HTTPS
server {
listen 80;
server_name mysecondwebsite.net;
access_log off; # No need for logging on this
error_log off;
return 301 https://mysecondwebsite.net$request_uri;
}

  server {
  listen  443 ssl;
  root  /var/opt/httpd/ifdocs;

server_name mysecondwebsite.net ;

  # add Strict-Transport-Security to prevent man in the middle attacks
add_header Strict-Transport-Security "max-age=31536000" always;
ssl on;
ssl_certificate     /etc/pki/tls/certs/cert.pem;
ssl_certificate_key /etc/pki/tls/certs/cert.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers         HIGH:!aNULL:!MD5;

access_log /var/log/nginx/iflogs/http/access.log;
error_log  /var/log/nginx/iflogs/http/error.log;


###include rewrites/default.conf;
index  index.php index.html index.htm;

# Make nginx serve static files instead of Apache
# NOTE this will cause issues with bandwidth accounting as files wont be logged
location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css)$ {
    expires max;
}

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
       proxy_pass https://127.0.0.1:4434;
}

# proxy the PHP scripts to Apache listening on <serverIP>:8080
location ~ \.php$ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
      proxy_pass https://127.0.0.1:4434;
}

location ~ /\. {
    deny  all;
}

error_page  500 502 503 504  /50x.html;
location = /50x.html {
    root  /usr/share/nginx/html;
}
}

然后您将使用ln -s /etc/nginx/sites-available/mysecondwebsite /etc/nginx/sites-available/创建一个符号链接并重新启动nginx。要回答有关端口的问题,您只能在一个端口上监听一个TCP应用程序。 This post提供了更多有关此的详细信息。

您还可以在服务器块中定义上游,如下所示:

 upstream mysecondwebsite {
     server 127.0.0.1:4434; # Or whatever port you use
 }

然后使用代理传递引用上游,如下所示:

proxy_pass http://mysecondwebsite;

这样,如果您更改端口,则只需在服务器配置文件中的一个位置进行更改。另外,这就是您使用多个Apache服务器扩展应用程序并实现负载平衡的方式。