utuntu上安裝nginx

前言

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好。(百度百科)

1.Nginx安裝

我使用的環境是64位 Ubuntu 14.04。nginx依賴以下模塊:

  • gzip模塊需要 zlib 庫
  • rewrite模塊需要 pcre 庫
  • ssl 功能需要openssl庫

1.1.安裝pcre

1.2.安裝openssl

1.3.安裝zlib

1.4.安裝nginx

下載pcre、openssl、zlib、及其nginx安裝包 下載地址:http://download.csdn.net/download/liu976180578/10134160
  1. 獲取pcre編譯安裝包     下載地址:http://download.csdn.net/download/liu976180578/10134160
  2. 解壓縮pcre-xx.tar.gz包。
  3. 進入解壓縮目錄,執行./configure。
  4. make & make install
  5. 獲取openssl編譯安裝包     下載地址:http://download.csdn.net/download/liu976180578/10134160
  6. 解壓縮openssl-xx.tar.gz包。
  7. 進入解壓縮目錄,執行./config。
  8. make & make install
  9. 獲取zlib編譯安裝包           下載地址:http://download.csdn.net/download/liu976180578/10134160
  10. 解壓縮openssl-xx.tar.gz包。
  11. 進入解壓縮目錄,執行./configure。
  12. make & make install
  13. 獲取nginx
  14. 解壓縮nginx-xx.tar.gz包。  下載地址:http://download.csdn.net/download/liu976180578/10134160
  15. 進入解壓縮目錄,執行./configure
  16. make & make install

安裝nginx注意事項

若安裝時找不到上述依賴模塊,使用--with-openssl=<openssl_dir>、--with-pcre=<pcre_dir>、--with-zlib=<zlib_dir>指定依賴的模塊目錄。

nginx解壓包下命令   ./configure  --with-openssl=<openssl_dir>  --with-pcre=<pcre_dir>  --with-zlib=<zlib_dir>


啓動nginx

安裝成功後,系統默然會把nginx放在/usr/local/nginx 目錄下   

進入/usr/local/nginx 目錄, 執行啓動命令  sbin/nginx


重啓nginx

必須cd 到sbin目錄,必須是在已啓動時執行

             cd  /usr/local/nginx/sbin   sudo

            ./nginx  -s reload



啓動nginx之後,瀏覽器中輸入http://localhost可以驗證是否安裝啓動成功。

Ubuntu 14.04下Nginx安裝與使用 

Nginx的簡單配置

  1. #Nginx創建進程所用用戶和用戶組  
  2. #user  nobody;  
  3. user  test  test;  
  4. #進程數,一般等於CPU數量  
  5. worker_processes  4;  
  6.   
  7.   
  8. #錯誤日誌,定義到warn級別比較合適  
  9. #error_log  logs/error.log;  
  10. error_log  logs/error.log  warn;  
  11.   
  12.   
  13. #指定pid存放文件  
  14. pid        logs/nginx.pid;  
  15.   
  16. events {  
  17.     #使用網絡IO模型linux建議epoll,FreeBSD建議採用kqueue  
  18.     use epoll;  
  19.   
  20.     #允許最大連接數  
  21.     worker_connections  1024;  
  22. }  
  23.   
  24. http {  
  25.     include       mime.types;  
  26.     default_type  application/octet-stream;  
  27.   
  28.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  29.     #                  '$status $body_bytes_sent "$http_referer" '  
  30.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  31.   
  32.     #關閉日誌能夠減少IO,但是建議開啓error_log warn級別  
  33.     #access_log  logs/access.log  main;  
  34.   
  35.     sendfile        on;  
  36.     #tcp_nopush     on;  
  37.   
  38.     #HTTP連接的持續時間  
  39.     keepalive_timeout  30;  
  40.   
  41.     #壓縮設置  
  42.     gzip  on;  
  43.     gzip_types  text/javascript text/plain text/css application/xml application/x-javascript;  
  44.   
  45.     #Tomcat集羣配置  
  46.     upstream myserver {  
  47.       #通過client的IP進行映射  
  48.       ip_hash;  
  49.       # 負載均衡設置,weight越大越優先分配  
  50.       server  localhost:18080  weight=5;  
  51.       server  localhost:18081  weight=4;  
  52.     }  
  53.   
  54.     server {  
  55.         listen       8080; #監聽的端口號  
  56.         server_name  localhost;  
  57.   
  58.         charset utf-8; #字符編碼方式  
  59.   
  60.         #關閉日誌能夠減少IO  
  61.         #access_log  logs/host.access.log  main;  
  62.   
  63.         location / {  
  64.             root   html;  
  65.             index  index.html index.htm;  
  66.   
  67.   
  68.             #代理配置  
  69.             proxy_pass         http://myserver;  
  70.             #關閉重定向  
  71.             #proxy_redirect     off;  
  72.         }  
  73.   
  74.         #error_page  404              /404.html;  
  75.   
  76.         # redirect server error pages to the static page /50x.html  
  77.         #  
  78.         error_page   500 502 503 504  /50x.html;  
  79.         location = /50x.html {  
  80.             root   html;  
  81.         }  
  82.   
  83.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  84.         #  
  85.         #location ~ \.php$ {  
  86.         #    proxy_pass   http://127.0.0.1;  
  87.         #}  
  88.   
  89.   
  90.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  91.         #  
  92.         #location ~ \.php$ {  
  93.         #    root           html;  
  94.         #    fastcgi_pass   127.0.0.1:9000;  
  95.         #    fastcgi_index  index.php;  
  96.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  97.         #    include        fastcgi_params;  
  98.         #}  
  99.   
  100.   
  101.         # deny access to .htaccess files, if Apache's document root  
  102.         # concurs with nginx's one  
  103.         #  
  104.         #location ~ /\.ht {  
  105.         #    deny  all;  
  106.         #}  
  107.     }  
  108.   
  109.     # another virtual host using mix of IP-, name-, and port-based configuration  
  110.     #  
  111.     #server {  
  112.     #    listen       8000;  
  113.     #    listen       somename:8080;  
  114.     #    server_name  somename  alias  another.alias;  
  115.   
  116.   
  117.     #    location / {  
  118.     #        root   html;  
  119.     #        index  index.html index.htm;  
  120.     #    }  
  121.     #}  
  122.   
  123.   
  124.     # HTTPS server  
  125.     #  
  126.     #server {  
  127.     #    listen       443;  
  128.     #    server_name  localhost;  
  129.   
  130.   
  131.     #    ssl                  on;  
  132.     #    ssl_certificate      cert.pem;  
  133.     #    ssl_certificate_key  cert.key;  
  134.   
  135.   
  136.     #    ssl_session_timeout  5m;  
  137.   
  138.   
  139.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  140.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  141.     #    ssl_prefer_server_ciphers   on;  
  142.   
  143.   
  144.     #    location / {  
  145.     #        root   html;  
  146.     #        index  index.html index.htm;  
  147.     #    }  
  148.     #}  
  149. }  

一個機器上運行多個tomcat : 

                                 http://blog.csdn.net/T_ZZZ/article/details/76906599 

redis緩存共享session   :

1、下載三個jar ,下載地址:http://download.csdn.net/download/liu976180578/10134194

     下好後,放入tomcat的lib裏面,每個tomcat 都要放。

2、打開每個tomcat 的    conf目錄下的context.xml文件,在最後一個節點</Context>上面添加。



< Valve    className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"  host="192.168.145.130"  port="7000"  database="0"  maxInactiveInterval="60" /> 

host是redis的主機,port是redis的端口號   其他不用管,最好用redis集羣

rides集羣管理session
<ValveclassName="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<ManagerclassName="com.radiadesign.catalina.session.RedisSessionManager"
maxInactiveInterval="60"
sentinelMaster="mymaster"
sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" />

Nginx詳情配置

安裝完成之後,配置目錄conf下有以下配置文件,過濾掉了xx.default配置:

linuxidc@ubuntu:/opt/nginx-1.7.7/conf$ tree |grep -v default

.

├── fastcgi.conf

├── fastcgi_params

├── koi-utf

├── koi-win

├── mime.types

├── nginx.conf

├── scgi_params

├── uwsgi_params

└── win-utf

除了nginx.conf,其餘配置文件,一般只需要使用默認提供即可

2.1.nginx.conf

nginx.conf是主配置文件,默認配置去掉註釋之後的內容如下圖所示:

l  worker_process表示工作進程的數量,一般設置爲cpu的核數

l  worker_connections表示每個工作進程的最大連接數

l  server{}塊定義了虛擬主機

n  listener監聽端口

n  server_name監聽域名

n  location{}是用來爲匹配的 URI 進行配置,URI 即語法中的“/uri/”。location  / { }匹配任何查詢,因爲所有請求都以 / 開頭。

u  root指定對應uri的資源查找路徑,這裏html爲相對路徑,完整路徑爲/opt/ opt/nginx-1.7.7/html/

u  index指定首頁index文件的名稱,可以配置多個,以空格分開。如有多個,按配置順序查找。

 

從配置可以看出,nginx監聽了80端口、域名爲localhost、跟路徑爲html文件夾(我的安裝路徑爲/opt/nginx-1.7.7,所以/opt/nginx-1.7.7/html)、默認index文件爲index.html, index.htm、服務器錯誤重定向到50x.html頁面。

可以看到/opt/nginx-1.7.7/html/有以下文件:

linuxidc@ubuntu:/opt/nginx-1.7.7/html$ ls

50x.html  index.html

這也是上面在瀏覽器中輸入http://localhost,能夠顯示歡迎頁面的原因。實際上訪問的是/opt/nginx-1.7.7/html/index.html文件。

2.2.mime.types

文件擴展名與文件類型映射表,nginx根據映射關係,設置http請求響應頭的Content-Type值。當在映射表找不到時,使用nginx.conf中default-type指定的默認值。例如,默認配置中的指定的default-type爲application/octet-stream。

    include      mime.types;

    default_type  application/octet-stream;

默認

下面截一段mime.types定義的文件擴展名與文件類型映射關係,完整的請自行查看:

 

2.3.fastcgi_params

nginx配置Fastcgi解析時會調用fastcgi_params配置文件來傳遞服務器變量,這樣CGI中可以獲取到這些變量的值。默認傳遞以下變量:

 

這些變量的作用從其命名可以看出。

2.4.fastcgi.conf

對比下fastcgi.conf與fastcgi_params文件,可以看出只有以下差異:

linuxidc@ubuntu:/opt/nginx-1.7.7/conf$ diff fastcgi.conf fastcgi_params

2d1

< fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

即fastcgi.conf只比fastcgi_params多了一行“fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;”

原本只有fastcgi_params文件,fastcgi.conf是nginx 0.8.30 (released: 15th of December 2009)才引入的。主要爲是解決以下問題(參考:

原本Nginx只有fastcgi_params,後來發現很多人在定義SCRIPT_FILENAME時使用了硬編碼的方式。例如,fastcgi_param SCRIPT_FILENAME /var/www/foo$fastcgi_script_name。於是爲了規範用法便引入了fastcgi.conf。

不過這樣的話就產生一個疑問:爲什麼一定要引入一個新的配置文件,而不是修改舊的配置文件?這是因爲fastcgi_param指令是數組型的,和普通指令相同的是:內層替換外層;和普通指令不同的是:當在同級多次使用的時候,是新增而不是替換。換句話說,如果在同級定義兩次SCRIPT_FILENAME,那麼它們都會被髮送到後端,這可能會導致一些潛在的問題,爲了避免此類情況,便引入了一個新的配置文件。

因此不再建議大家使用以下方式(搜了一下,網上大量的文章,並且nginx.conf的默認配置也是使用這種方式):

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

而使用最新的方式:

include fastcgi.conf;

 

2.5.uwsgi_params

與fastcgi_params一樣,傳遞哪些服務器變量,只有前綴不一樣,以uwsgi_param開始而非fastcgi_param

2.6.scgi_params

與fastcgi_params一樣,傳遞哪些服務器變量,只有前綴不一樣,以uwsgi_param開始而非fastcgi_param

2.7.koi-utf、koi-win、win-utf

這三個文件都是與編碼轉換映射文件,用於在輸出內容到客戶端時,將一種編碼轉換到另一種編碼。

koi-win: charset_map  koi8-r < -- > windows-1251

koi-utf: charset_map  koi8-r < -- > utf-8

win-utf: charset_map  windows-1251 < -- > utf-8

koi8-r是斯拉夫文字8位元編碼,供俄語及保加利亞語使用。在Unicode未流行之前,KOI8-R 是最爲廣泛使用的俄語編碼,使用率甚至起ISO/IEC 8859-5還高。

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