Nginx核心要領十四:Nginx安裝配置完整版

分兩種方式介紹安裝nginx,第一種服務器上直接安裝nginx,第二種docker安裝nginx
1.服務器上直接安裝
1.1.更新與安裝編譯工具

yum update
yum -y install vim gcc gcc-c++ automake autoconf libtool make pcre-devel openssl openssl-devel zlib zlib-devel

1.2.下載nginx

wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -xzvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
mkdir module

1.3.把要安裝的插件放入 nginx-1.16.0/module/ 下

nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar
nginx-http-concat-1.2.2.tar

1.4.編譯,把插件添加進去一起編譯
 ./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-1.16.0/module/nginx-http-concat-1.2.2/ --add-module=/usr/local/nginx-1.16.0/module/nginx-sticky-module/ --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_v2_module
1.5.安裝

make && make install

1.6.配置nginx.conf
worker_processes  2;
events {
    worker_connections  10240;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length 2k;
    gzip_buffers 4 16k;
    gzip_comp_level 3;
    gzip_types text/plain text/css text/javascript application/javascript application/x-javascript application/xml application/x-httpd-php application/x-font-ttf font/ttf font/eot image/jpeg image/gif image/png;

    upstream xxx {
       #使用sticky,不設置expires則瀏覽器關閉時結束會話
       #sticky domain=xxx.zypcy.cn path=/;
       server localhost:8080;
       keepalive 32;
       keepalive_requests 100;
       keepalive_timeout 40s;
    }

    server {
        listen       80;
        server_name  xxx.zypcy.cn;
        location / {
            proxy_pass http://xxx;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_buffer_size 4k;
            proxy_buffers 4 32k;
            client_max_body_size 10m;
            client_body_buffer_size 256k;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    #配置concat合併小文件功能
    #http://192.168.68.136:8080/js/??a.js,b.js,c.js (同一目錄下資源)
    #http://192.168.68.136:8080/??kissy/seed-min.js,kg/global-util/index-min.js(不同目錄下資源)
    server {
        listen       80;
        server_name  res.zypcy.cn;
        #允許跨域訪問
        add_header 'Access-Control-Allow-Origin' '*';
        location / {
	    #開啓referers防盜鏈,只能 *.zypcy.cn 能訪問該location下的資源
            valid_referers none blocked server_names *.zypcy.cn;
            if ($invalid_referer) {
             #盜鏈時返回403
             return 403;
            }
            alias /home/lkh/;
            index  index.html index.htm;
            
            #開啓小文件合併功能
            concat on;
            concat_max_files 30;
            concat_types concat_types: text/css text/javascript application/javascript application/x-javascript;
        }
    } 
}
1.7.系統參數優化

vim /etc/sysctl.conf

net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30

配置生效 :sysctl -p

1.8.更改進程最大文件句柄數:ulimit -n 1048576

vi /etc/profile
加入:ulimit -SHn 1048576
source /etc/profile

2.Docker安裝Nginx
2.1.安裝Docker
2.2.下載nginx鏡像,docker pull nginx:1.16.1
2.3.在服務器上創建2個目錄,用於存放nginx.conf和日誌,mkdir -p /home/nginx/logs /home/nginx/conf
2.4.可以先運行nginx鏡像,把容器中的nginx.conf複製出來,或者參照官網寫nginx.conf,這裏先運行 docker run -d --name mynginx nginx:1.16.1,然後 docker ps 查看nginx容器的ID,通過 cp 命令把容器中的nginx.conf複製到指定目錄, docker cp 13afb35cbc98:/etc/nginx/nginx.conf /home/nginx/conf

根據自己的需求修改 vi /home/nginx/conf/nginx.conf 配置中的內容

user  nginx;
worker_processes  4;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  10240;
}
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    
    gzip  on;
    gzip_min_length 2k;
    gzip_buffers 4 16k;
    gzip_comp_level 3;
    gzip_types text/plain text/css text/javascript application/javascript application/x-javascript application/xml application/x-httpd-php application/x-font-ttf font/ttf font/eot image/jpeg image/gif image/png;

    include /etc/nginx/conf.d/*.conf;

  server {
    listen 80;
    server_name 10.10.10.146;
    location / {
      proxy_pass http://127.0.0.1:8080;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header REMOTE-HOST $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      #add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
      client_max_body_size 50m;
      client_body_buffer_size 512k;
      proxy_connect_timeout 1;
      proxy_send_timeout 60;
      proxy_read_timeout 120;
      proxy_buffer_size 256k;
      proxy_buffers 4 256k;
      proxy_busy_buffers_size 256k;
      proxy_temp_file_write_size 256k;
      proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
      proxy_max_temp_file_size 128m;
    }
  }
}
2.5.再次運行nginx鏡像

–restart=always 表示docker重啓時,容器也重啓
–network=host 表示使用宿主機網絡

docker run -d  --restart=always -it --name nginx -p 80:80 \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /home/nginx/logs:/var/log/nginx \
--network=host nginx:1.16.1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章