簡體   English   中英

如何在同一服務器中處理從nginx到apache的多個主機名句柄?

[英]How to handle multiple hostnames handles from nginx to apache in the same server?

我計划在同一台服務器上管理多個網站,目前正在處理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)為多台服務器提供服務還是為每個網站使用不同的端口是否會出現問題。

先感謝您。

多個服務器配置

一種方法是擁有多個服務器塊,最好是在不同的conf文件上。 這樣的事情會對新文件(例如/etc/nginx/sites-available/mysecondwebsite )中的第二台服務器/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應用程序。 這篇文章提供了更多有關此的詳細信息。

您還可以在服務器塊中定義上游,如下所示:

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

然后使用代理傳遞引用上游,如下所示:

proxy_pass http://mysecondwebsite;

這樣,如果您更改端口,則只需在服務器conf中的一個位置進行更改。 另外,這就是您使用多個Apache服務器擴展應用程序並實現負載平衡的方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM