Nginx+Tomcat的服務器端環境配置詳解動靜分離

最近學習nginx先拿windos練練手,稍微有些積累記錄下

1. 先去網上下載nginx後解壓,我這邊放到F:nginx下了;
2. 點擊nginx目錄下想nginx.exe按照,閃了一下;
3. 打開瀏覽器,輸入localhost看看是否有welcome to nginx字樣;或是打開任務管理器,進程中是否有nginx.exe或是打開        nginx的安裝目錄看logs下面的error.log大小是否是0kb;
4. 如果能正常啓動則繼續,如果啓動不了看看error.log提示內容是什麼; 因爲nginx默認監聽80端口,可能會出現端口占用的情況, 改下conf中的nginx.conf文件就好,在
http{
server{
 listen 8077;  # 監聽8077端口
}
}下面會介紹;我在啓動的時候,因爲安裝了php,所以發生了端口占用這樣的事情;
5. 因爲是測試本地的服務,所以修改hosts,新增 127.0.0.1    a.com;
6. 我在本地和外網準備了2臺服務器;
7. 修改修改下tomcat的server.xml文件,除了配置端口等外,在
<Host> <Context path="" docBase="/zjlm.client.web" reloadable="true"/> </Host>
8. 完整的nginx.conf文件內容是


#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;
server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;     
    large_client_header_buffers 4 32k;
    
    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    upstream a.com {
         server 60.205.141.136:8080 weight=1;  # 服務器1
         server 127.0.0.1:8087 weight=1;   #服務器2
    } 


    server {
        listen       8077;    #監聽端口
        server_name  a.com 
root F:/nginx/nginx/nginx/html;
proxy_redirect off;
        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
           root   html;
           index  index.jsp index.html index.htm;
  #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP
  proxy_set_header Host $host;
  #防止ajax安全請求問題
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  #禁用緩存
           proxy_buffering off;
  #設置反向代理的地址
  proxy_pass http://a.com;
  
        }

location ~ \.(jsp|jspx|do|action)(\/.*)?$ {
              index index.jsp;
              proxy_set_header  Host $host;  
              proxy_set_header  X-Real-IP  $remote_addr;
              proxy_pass http://a.com;
           } 

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
root F:/nginx/nginx/nginx/html;

}

location ~ .*\.(html|js|css|png|gif)$ {
root F:/nginx/nginx/nginx/html;

}



        #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 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;
    #    }
    #}


}
9. 需要注意的事情, 一開始配置完靜態文件不能訪問,靜態資源路徑不對, 需要把靜態資源拷到nginx目錄下,然後配置root 文件目錄;
10. 訪問a.com:8077查看情況

11. #注* server段 proxy_pass定義的a.com需要跟upstream 裏面定義的a.com一致,否則server找不到均衡。
12. #配置Nginx動靜分離   
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root F:/nginx/nginx/nginx/html;
}
13. #expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力
expires 3d; 
14. 開啓nginx的監控
nginx簡單狀態監控,在nginx.conf中添加如下代碼即可監控nginx當前的狀態,然後訪問http://serverip/status即可訪問
location /status {
        stub_status on;
access_log off;
}
一般顯示爲
Active connections: 16 
server accepts handled requests
191226 191226 305915
Reading: 0 Writing: 1 Waiting: 15

ctive connections: 對後端發起的活動連接數.

Server accepts handled requests: Nginx總共處理了24個連接,成功創建24次握手(證明中間沒有失敗的),總共處理了129個請求.

Reading: Nginx 讀取到客戶端的Header信息數.

Writing: Nginx 返回給客戶端的Header信息數.

Waiting: 開啓keep-alive的情況下,這個值等於 active – (reading + writing),意思就是Nginx已經處理完成,正在等候下一次請求指令的駐留連接.
注意的,本模塊默認是不會編譯進Nginx的,如果你要使用該模塊,則要在編譯安裝Nginx時指定:

參考網站:http://www.jb51.net/article/77078.htm







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章