負載均衡(三)Nginx的安裝配置

linux系統爲Centos 64位javascript

1、安裝

[root@cuiqq local]# mkdir /usr/local/nginx
[root@cuiqq local]# cd /usr/local/nginx/php

[root@cuiqq nginx]# wget http://nginx.org/download/nginx-1.17.5.tar.gzcss

[root@cuiqq nginx]# tar -zxvf nginx-1.17.5.tar.gzhtml

[root@cuiqq nginx]# cd nginx-1.17.5java

[root@cuiqq nginx-1.17.5]# ./configure node

[root@cuiqq nginx-1.17.5]# make installlinux

[root@cuiqq bin]# cd /usr/local/nginx/nginx

[root@cuiqq nginx]# rm -rf nginx-1.17.5 nginx-1.17.5.tar.gzweb

[root@cuiqq nginx]# cp sbin/nginx /usr/local/bin/json

[root@cuiqq nginx]# nginx

[root@cuiqq nginx]# ps -ef | grep nginx
root 19784 1 0 00:48 ? 00:00:00 nginx: master process nginx
nobody 19785 19784 0 00:48 ? 00:00:00 nginx: worker process
root 20146 20751 0 00:48 pts/1 00:00:00 grep nginx

======安裝完成=====

2、nginx命令

進入/sbin 目錄後執行命令:

nginx啓動 1、啓動nginx:nginx 2、關閉nginx:nginx -s stop 3、重啓nginx:nginx -s reload

多種中止方式:

中止操做是經過向nginx進程發送信號來進行的

查詢nginx主進程號

ps -ef | grep nginx
root 20191 1 0 21:00 ? 00:00:00 nginx: master process nginx
nobody 20192 20191 0 21:00 ? 00:00:00 nginx: worker process

在進程列表裏 面找master進程,它的編號就是主進程號了。

發送信號從容中止Nginx

kill -QUIT 主進程號

快速中止Nginx

kill -TERM 主進程號

強制中止Nginx

pkill -9 nginx

另外, 若在nginx.conf配置了pid文件存放路徑則該文件存放的就是Nginx主進程號,若是沒指定則放在nginx的logs目錄下。有了pid文 件,咱們就不用先查詢Nginx的主進程號,而直接向Nginx發送信號了,命令以下:
kill -信號類型 '/usr/nginx/logs/nginx.pid'

平滑重啓

若是更改了配置就要重啓Nginx,要先關閉Nginx再打開?不是的,能夠向Nginx 發送信號,平滑重啓。
平滑重啓命令:
kill -HUP 主進程號或進程號文件路徑

或者使用

/usr/nginx/sbin/nginx -s reload

注意,修改了配置文件後最好先檢查一下修改過的配置文件是否正 確,以避免重啓後Nginx出現錯誤影響服務器穩定運行。

判斷Nginx配置是否正確命令

nginx -t -c /usr/nginx/conf/nginx.conf

或者

/usr/nginx/sbin/nginx -t
 
[root@cuiqq conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

3、配置nginx.conf

nginx配置分爲五大區域,main、event、http、service、location。咱們經常使用的是後面三個

 

做用:全局做用域

    ##全局塊 開始## 

        user nobody; #配置容許運行nginx服務器的用戶和用戶組,這裏的nobody就是上面查看nginx進程時顯示的nobody 

        worker_processes number  | auto ; #nginx進程參數,一般設置成和CPU的數量相等 也能夠設置成auto 演示v5

        error_log logs/error.log; #錯誤日誌存放路徑 

        #error_log logs/error.log notice; 

        #error_log logs/error.log info; 

        pid logs/nginx.pid; #nginx pid記錄位置 

    ##全局塊 結束##

Event塊配置

    做用:Nginx服務器與用戶的網絡鏈接

    events {

        worker_connections  1024; #鏈接數上限,單個後臺進程最大併發連接數默認1024

        use epoll; #工做模式:epoll多路複用IO中的一種方式,僅適用於linux2.6以上內核,提升nginx性能 uname –a 查看linux內核

    }

 

1 user www www; 2 # 工做進程個數,可配置多個 3 worker_processes auto; 4 
  5 error_log /data/wwwlogs/error_nginx.log crit; 6 pid /var/run/nginx.pid; 7 worker_rlimit_nofile 51200; 8 
  9 events { 10 use epoll; 11 # 單個進程最大鏈接數 12   worker_connections 51200; 13 multi_accept on; 14 } 15 
 16 http { 17 include mime.types; 18   default_type application/octet-stream; 19   server_names_hash_bucket_size 128; 20 client_header_buffer_size 32k; 21   large_client_header_buffers 4 32k; 22 client_max_body_size 1024m; 23 client_body_buffer_size 10m; 24 sendfile on; 25 tcp_nopush on; 26   keepalive_timeout 120; 27 server_tokens off; 28 tcp_nodelay on; 29 
 30   fastcgi_connect_timeout 300; 31   fastcgi_send_timeout 300; 32   fastcgi_read_timeout 300; 33 fastcgi_buffer_size 64k; 34   fastcgi_buffers 4 64k; 35 fastcgi_busy_buffers_size 128k; 36 fastcgi_temp_file_write_size 128k; 37 fastcgi_intercept_errors on; 38 
 39 #Gzip Compression 40 gzip on; 41   gzip_buffers 16 8k; 42   gzip_comp_level 6; 43   gzip_http_version 1.1; 44   gzip_min_length 256; 45 gzip_proxied any; 46 gzip_vary on; 47 gzip_types 48     text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml 49     text/javascript application/javascript application/x-javascript 50     text/x-json application/json application/x-web-app-manifest+json 51     text/css text/plain text/x-component 52     font/opentype application/x-font-ttf application/vnd.ms-fontobject 53     image/x-icon; 54   gzip_disable "MSIE [1-6]\.(?!.*SV1)"; 55 
 56   #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency. 57   open_file_cache max=1000 inactive=20s; 58 open_file_cache_valid 30s; 59   open_file_cache_min_uses 2; 60 open_file_cache_errors on; 61 
 62 ######################## default ############################ 63 # 服務器集羣名稱自定義的 和下面的location地址對應 64 upstream myServer { 65 # weigth參數表示權值,權值越高被分配到的概率越大 66     # server 127.0.0.1:8080 weight=1; 67     # server 127.0.0.1:8060 weight=1; 68     server 47.93.10.184:8080; 69     server 47.93.10.184:8081;
 71 } 72 
 73 # 每個server至關於一個代理服務器 74 server { 75 # 監聽端口,默認80 76   listen 8848; 77 # 當前服務的域名,能夠有多個,用空格分隔(咱們是本地因此是localhost) www.cuiqq.cn 也能夠是 ip 78 server_name localhost; 79 #server_name _; 80   access_log /data/wwwlogs/access_nginx.log combined; 81   root /data/wwwroot/default; 82 # 當沒有指定主頁時,默認會選擇這個指定的文件,可多個,空格分隔 83 index index.html index.htm index.php; 84   # 表示匹配的路徑,這時配置了/表示全部請求都被匹配到這裏 ***重點:路徑匹配規則會有單獨一節說明*** 85   location / { 86 # 請求轉向自定義的服務器列表 87       proxy_pass http://myServer;
 88 } 89   location /nginx_status { 90 stub_status on; 91 access_log off; 92     allow 127.0.0.1; 93 deny all; 94 } 95   location ~ [^/]\.php(/|$) { 96     #fastcgi_pass remote_php_ip:9000; 97     fastcgi_pass unix:/dev/shm/php-cgi.sock; 98 fastcgi_index index.php; 99 include fastcgi.conf; 100 } 101   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { 102 expires 30d; 103 access_log off; 104 } 105   location ~ .*\.(js|css)?$ { 106 expires 7d; 107 access_log off; 108 } 109   location ~ /\.ht { 110 deny all; 111 } 112 } 113 
114 ########################## conf.d ############################# 115   include conf.d/*.conf; ##將另一個文件包含到當前文件中,通常會放一些 upstream 116 }
相關文章
相關標籤/搜索