1. 程式人生 > >nginx代理雙域名,同一個IP地址的伺服器實現兩個域名配置

nginx代理雙域名,同一個IP地址的伺服器實現兩個域名配置

作業系統環境:win2003Server

安裝軟體:Nginx

環境:安裝兩個tomcat服務,一個為9001埠、一個為9002埠,nginx預設為80埠

#目的:實現一個80埠,兩個域名同時可以訪問,nginx作為代理接收80埠的資料,轉發到後端的tomcat服務處理

1.安裝nginx  網上很多方法

2.配置檔案:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80  default;
        server_name  *.baidu.com;
 #rewrite ^/(.*) http://www.baidu.com/$1 permanent;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           proxy_pass http://localhost:9001/;
    proxy_set_header   Host    $host;
           proxy_set_header   X-Real-IP   $remote_addr;
           proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for; 

   
        }

        #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   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
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
        #}
    }

    #another virtual host use 9002 port
     server {
        listen       80;
        server_name  *.sina.com;
        #rewrite ^/(.*) http://www.sina.com/$1 permanent;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           proxy_pass http://localhost:9002/;
    proxy_set_header   Host    $host;
           proxy_set_header   X-Real-IP   $remote_addr;
           proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
   
 
   
         }
        }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3.測試,根據上面的配置檔案,測試成功

4.配置windows下nginx服務自啟動

參考了其官網文件:瞭解到它的基本啟停命令如下:(假設,解壓到了 "F:\nginx-0.9.4" 目錄下)
 
# 啟動:
F:\nginx-0.9.4> nginx.exe
 
# 檢視狀態:
F:\nginx-0.9.4> tasklist /fi "imagename eq nginx.exe"
 
# 立即停止:
F:\nginx-0.9.4> nginx.exe -s stop
 
# 結束所有會話後再停止(平滑退出):
F:\nginx-0.9.4> nginx.exe -s quit
 
# 重新載入配置檔案
# (當修改配置檔案後,他會先啟動一個新的worker,在把原來的worker停止,也即對使用者透明)
F:\nginx-0.9.4> nginx.exe -s reload
 
# 重新開啟日誌檔案
F:\nginx-0.9.4> nginx.exe -s reopen
 
 
只可惜,nginx.exe自己不管註冊windows 伺服器,需要自己手動命令列下啟動/停止。
用Google百度了下,發現一個不錯的文章——《Nginx Windows Service》,同時在文章裡也發現了
一個不錯的程式:"Windows Service Wrapper" —— 用來將你的程式封裝成Windows的服務。
你可以在這裡下載。
 
使用方法:
1. 下載最新版的 Windows Service Wrapper 程式,比如我下載的名稱是 "winsw-1.9-bin.exe",
    然後,把它命名成你想要的名字(比如: "myapp.exe",當然,你也可以不改名)
2. 將重新命名後的 myapp.exe 複製到 nginx 的安裝目錄(我這裡是 "F:\nginx-0.9.4")
3. 在同一個目錄下建立一個Windows Service Wrapper的XML配置檔案,名稱必須與第一步重新命名時使用的名稱一致(比如我這裡是 "myapp.xml",  如果,你沒有重新命名,則應該是 "winsw-1.9-bin.xml")
   檔案內容如下:
 
<?xml version="1.0" encoding="UTF-8" ?>
<service>
  <id>nginx</id>
  <name>nginx</name>
  <description>nginx</description>
  <executable>F:\nginx-0.9.4\nginx.exe</executable>
  <logpath>F:\nginx-0.9.4\</logpath>
  <logmode>roll</logmode>
  <depend></depend>
  <startargument>-p F:\nginx-0.9.4</startargument>
  <stopargument>-p F:\nginx-0.9.4 -s stop</stopargument>
</service>
4. 命令列下執行以下命令,以便將其安裝成Windows服務。
F:\nginx-0.9.4> myapp.exe install
 
OK,至此,完工,確認一下:我的電腦 右鍵 -> 管理 -> 服務 -> 是否有了個 nginx 呢?啟動起來,在瀏覽器中 訪問 http://localhost 確認一下,再 停止服務,再訪問一下  http://localhost 確認一下。
 
補充:
Windows Servcie Wrapper的命令格式如下:
 
# 安裝服務
CMD:\> myapp.exe install
 
# 解除安裝服務
CMD:\> myapp.exe uninstall
 
# 啟動服務
CMD:\> myapp.exe start
 
# 停止服務
CMD:\> myapp.exe stop