====== CentOS 安裝 Nginx 擔任網站分派器 ======
===== 安裝 nginx yum repo =====
==== CentOS 6 ====
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
==== CentOS 7 ====
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
===== 安裝 nginx web-server =====
yum install nginx
===== 設定開機自動啟動 nginx =====
chkconfig nginx on
===== 設定 Virtual Host =====
* 假設連上 nginx 有 abc.ichiayo.com 與 xyz.ichiayi.com 兩個 Virtual Host 分別對應到 192.168.11.233 / 192.168.11.232
vi /etc/nginx/nginx.conf
:
worker_processes 2;
:
gzip on;
include /etc/nginx/conf.d/*.conf;
}
vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
:
vi /etc/nginx/conf.d/abc_ichiayi.conf
server {
server_name abc.ichiayi.com abc1.ichiayi.com;
access_log /var/log/nginx/abc.ichiayi.com.access.log main;
error_log /var/log/nginx/abc.ichiayi.com.error.log;
location / {
set_real_ip_from 192.168.11.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
proxy_pass http://192.168.11.233:80;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /292/ {
proxy_pass http://192.168.11.292/;
}
}
vi /etc/nginx/conf.d/xyz_ichiayi.conf
server {
server_name xyz.ichiayi.com;
access_log /var/log/nginx/xyz.ichiayi.com.access.log main;
error_log /var/log/nginx/xyz.ichiayi.com.error.log;
location / {
set_real_ip_from 192.168.11.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
proxy_pass http://192.168.11.232:80;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
* 當設定多個 Virtual Host 之後, 如果 DNS 有設定, 但未設定 Virtual Host 的 Domain Name 希望能指定預設網站, 可在預設的 server 設定檔內設定如下:
server {
listen 192.168.1.2:80 default_server;
server_name example.com www.example.com;
...
}
* 也就是在 listen 80 後面加上 **default_server**
* https 也可以這樣指定 Exp.
:
listen 443 ssl default_server;
:
* 因有朋友告知 http://ichiayi.com 出現 404 找不到網頁狀況, 所以針對 /etc/nginx/conf.d/www_ichiayi.conf 進行修改, 將 ichiayi.com 加入 server 內和 www.ichiayi.com 一樣
server {
server_name ichiayi.com www.ichiayi.com;
:
:
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.ichiayi.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = ichiayi.com) {
return 301 https://www.ichiayi.com$request_uri;
}
listen 80 default_server;
server_name ichiayi.com www.ichiayi.com;
return 404; # managed by Certbot
}
然後重新啟動 nginx
systemctl restart nginx
===== 其他相關資訊 =====
* **[[tech/ssl_letsencrypt]]**
* **[[tech/nginx_proxy_real_ip]]**
===== 參考網址 =====
* http://www.cyberciti.biz/faq/install-nginx-centos-rhel-6-server-rpm-using-yum-command/
* http://www.php100.com/html/program/nginx/2013/0905/5516.html
* http://nginx.org/en/docs/http/request_processing.html
* https://ma.ttias.be/nginx-proxy-upstream-sent-big-header-reading-response-header-upstream/
{{tag>install nginx centos web}}