差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

兩邊的前次修訂版 前次修改
下次修改
前次修改
tech:nginx [2019/04/19 13:49] jonathan_tsaitech:nginx [2021/03/17 14:14] (目前版本) – [設定 Virtual Host] jonathan
行 1: 行 1:
 +====== CentOS 安裝 Nginx 擔任網站分派器 ======
 +===== 安裝 nginx yum repo =====
 +==== CentOS 6 ==== 
 +<code sh>
 +rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
 +</code>
  
 +==== CentOS 7 ====
 +<code sh>
 +rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
 +</code>
 +
 +===== 安裝 nginx web-server =====
 +<code sh>
 +yum install nginx
 +</code>
 +
 +===== 設定開機自動啟動 nginx ===== 
 +<code sh>
 +chkconfig nginx on
 +</code>
 +
 +===== 設定 Virtual Host =====
 +  * 假設連上 nginx 有 abc.ichiayo.com  與 xyz.ichiayi.com 兩個 Virtual Host 分別對應到 192.168.11.233 / 192.168.11.232
 +<code sh>
 +vi /etc/nginx/nginx.conf
 +</code><file>
 +:
 +worker_processes  2;
 +:
 +    gzip  on;
 +
 +    include /etc/nginx/conf.d/*.conf;
 +}
 +</file><code sh>
 +vi /etc/nginx/conf.d/default.conf
 +</code><file>
 +server {
 +    listen       80;
 +    server_name  localhost;
 +:
 +</file><code sh>
 +vi /etc/nginx/conf.d/abc_ichiayi.conf
 +</code><file>
 +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/;
 +        }
 +}
 +</file><code sh>
 +vi /etc/nginx/conf.d/xyz_ichiayi.conf
 +</code><file>
 +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;
 +        }
 +}
 +</file>
 +
 +<note>
 +  * 當設定多個 Virtual Host 之後, 如果 DNS 有設定, 但未設定 Virtual Host 的 Domain Name 希望能指定預設網站, 可在預設的 server 設定檔內設定如下:<file>
 +server {
 +    listen      192.168.1.2:80 default_server;
 +    server_name example.com www.example.com;
 +    ...
 +}
 +</file>
 +  * 也就是在 listen 80 後面加上 **default_server**
 +  * https 也可以這樣指定 Exp. <file>
 +:
 +listen 443 ssl default_server;
 +:
 +</file>
 +</note>
 +
 +
 +<WRAP center round info 80%>
 +  * 因有朋友告知 http://ichiayi.com 出現 404 找不到網頁狀況, 所以針對 /etc/nginx/conf.d/www_ichiayi.conf 進行修改, 將 ichiayi.com 加入 server 內和 www.ichiayi.com 一樣<file>
 +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
 +}
 +</file>然後重新啟動 nginx <cli>
 +systemctl restart nginx
 +</cli>
 +</WRAP>
 +
 +===== 其他相關資訊 =====
 +  * **[[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}}