close

為了種種需求所以需要多台獨立伺服器及domain url,但設備只有寬頻路由1台、PC1台和一個固定IP,

架構以 Windows 2008 srv 當 HOST,在其上建置多台 VM Guest 跑 CentOS

Windows srv 跑 IIS , .NET + MSSQL + FTP ( port mapping 21)

VM Centos 7 + Nginx reserve proxy ( port mapping 80, 443)

VM Centos web server.....

大概就像這樣

reverse_proxy.png

先安裝 Nginx

方法一:

# sed -i /etc/selinux/config -r -e 's/^SELINUX=.*/SELINUX=disabled/g'   # 關閉 SELinux
# yum -y install epel-release   # Install EPEL repo
# yum -y update       # 更新一下
# systemctl reboot   # 更新後重啟,並讓 SELinux 設定生效

方法二:

直接使用 yum 安装,需要先更新一下 yum 源:
1. 在  /etc/yum.repos.d 文件夾下新建一個 nginx.repo 文件
# cd /etc/yum.repos.d
# vi nginx.repo

在 nginx.repo 中添加以下内容 :
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1

查看 yum 是否完成更新:
# yum list| grep nginx
 
返回内容(可能因為版本不同而有差異)
    nginx.i386                                    1.10.1-1.el6.ngx          @nginx 
    nginx-debug.i386                        1.8.0-1.el6.ngx             nginx   
    nginx-debuginfo.i386                  1.10.1-1.el6.ngx           nginx   
    nginx-module-geoip.i386            1.10.1-1.el6.ngx           nginx   
    ..............................

安裝 nginx

# yum install -y nginx

啟動並設成開機啟動

# systemctl start nginx.service
# systemctl enable nginx.service

查看 nginx 狀態

# systemctl status nginx

發現錯誤 systemd 1 failed to read pid from file /run/nginx.pid invalid argument
可能因為 nginx 啟動較慢,系統仍抓不到 PID ,查一下 /run/nginx.pid 是否存在
如果存在的話更改一下 /etc/nginx/nginx.conf
pid /var/run/nginx.pid; -> pid /run/nginx.pid

/*****************************************************************************************************/
Nginx 設定檔路徑(不同的安裝方式可能產生不同的位置)

# nginx -t    # 此指令可以檢查 nginx.conf 語法是否正確
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
表示設定檔路徑為 /etc/nginx/nginx.conf

/*****  編輯 Nginx 設定檔  ****************************/

# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig_copy # 備份nginx.conf

# vi nginx.conf
Nginx 服務的主要設定檔內容。

# 啟用 Nginx 的 Linux 帳戶
user  nginx;

# 啟用的執行緒數量(建議搭配 CPU 核心數 * 2)
worker_processes  1; # auto 

# log 檔位置
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {

# 允許同一時間連線的數量
worker_connections  1024;

}

http {

include       /etc/nginx/mime.types;
default_type  application/octet-stream;

          # 預設 log 記錄的格式
          log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                        '$status $body_bytes_sent "$http_referer" '
                                        '"$http_user_agent" "$http_x_forwarded_for"';

# access log 檔位置
access_log  /var/log/nginx/access.log  main;

sendfile        on;
# tcp_nopush     on;

keepalive_timeout  65;

# 是否啟用 gaip 壓縮(預設為註解,不啟用)
# gzip  on;

# 載入該路徑的所有設定檔,通常就是每個虛擬主機的配置
include /etc/nginx/conf.d/*.conf;

# 設定可上傳最大檔案容量(依需求設定)
# client_max_body_size 5m;    

#         server {

#                      # 這個主機的 Port
#                      listen       80;
#                      # 這個主機的名稱
#                      server_name  localhost;

#                      # 設定預設編碼,但通常都是由網頁 <meta> 來定義,因此預設註解
#                      # charset koi8-r;
#                      # 針對這個主機的 log 檔位置
#                      #access_log  /var/log/nginx/log/host.access.log  main;

#                      # html 檔
#                      location / {

#                                        # 網站的根目錄位置
#                                        root   /usr/share/nginx/html;
#                                        # 使用「瀏覽器」瀏覽根目錄時,未指定檔名時預設使用的檔案
#                                        index  index.html index.htm;

#                      }

#                     # 發生 404 指定導向哪個網頁
#                     # error_page  404              /404.html;

#                     # redirect server error pages to the static page /50x.html
#                     error_page   500 502 503 504  /50x.html;
#                     location = /50x.html {
#                                                          root   /usr/share/nginx/html;
#                     }

#                     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#                     # location ~ \.php$ {
#                     #                                 proxy_pass   http://127.0.0.1;
#                     # }

#                     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#                     # php 檔
#                     # location ~ \.php$ {
#                                                   #    root           html;
#                                                   #    fastcgi_pass   127.0.0.1:9000;
#                                                   #    fastcgi_index  index.php;
#                                                   #    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
#                                                   #    include        fastcgi_params;
#                                                   # }  

#                     # deny access to .htaccess files, if Apache's document root
#                     # concurs with nginx's one
#                     # location ~ /\.ht {
#                                                 # deny  all;
#                     # }
#         }    
         server {

listen 80;
server_name abc.123.com; # 連進來的 url

location / {

# 設定真實伺服器 轉向的 http:// ip:port
proxy_pass http://192.168.1.15:80;

# 將Host設定為使用者訪問時使用的網域(aa.example.com),
# 避免真實伺服器用網域作虛擬主機(Name-based Virtual Host)時無法作用
proxy_set_header Host $host;

# 自訂一個header變數,名稱可隨意設定,帶上使用者的 IP 
proxy_set_header X-Real-IP $remote_addr;

# 加上中間經過的代理IP,逗號區隔(client, proxy1, proxy2...)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
# proxy_max_temp_file_size 0;

}

}

server {

listen 80;
server_name xxx.456.com;

location / {

proxy_pass http://192.168.1.11:80;

}

}

server {

listen 80;
server_name yyy.789.com;

location / {

proxy_pass http://192.168.1.12:80;

}

}

}

 

收工..

arrow
arrow
    文章標籤
    Nginx reserve proxy
    全站熱搜

    Godspeed 發表在 痞客邦 留言(0) 人氣()