当前位置:   article > 正文

Nginx核心要领十四:Nginx安装配置完整版_nxgⅹ

nxgⅹ
分两种方式介绍安装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
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;
            #如果swagger没有转发端口,则可以用下面的配置
            #proxy_set_header Forwarded host=$host:$server_post;
            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;
            #下面2个配置参数在转发时比较重要,如果是api服务请注释,如果是页面服务请打开注释
            #proxy_buffering off;
            #proxy_redirect off;
        }

        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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

配置生效 :sysctl -p

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

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

2.Docker安装Nginx
2.1.安装Docker,下载nginx镜像,docker pull nginx:1.16.1
1.创建nginx目录结构:mkdir -p /home/nginx/logs /home/nginx/conf
2.给nginx目录授权:chmod 777 /home/nginx -R
3.启动nginx:docker run -d --name nginx nginx:1.16.1
4.把nginx容器中配置文件复制到宿主机:
  docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf
  docker cp nginx:/etc/nginx/conf.d/default.conf /home/nginx/conf

5.停止nginx:docker stop nginx; docker rm nginx;
6.启动nginx:docker run -d -it --name nginx --restart=always -p 7080:80 \
            -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
	        -v /home/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf
	        -v /home/nginx/logs:/var/log/nginx \
	        nginx:1.16.1

7.修改 /home/nginx/conf/default.conf 中的配置
8.热更新配置:docker exec -i nginx(容器id) nginx -s reload
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

根据自己的需求修改 vi /home/nginx/conf/default.conf 配置中的内容

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
          #root   /usr/share/nginx/html;
          #index  index.html index.htm;
          #default_type text/html;
          #return 200 'hello zy!';
	      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';
	      #如果swagger没有转发端口,则可以用下面的配置
	      #proxy_set_header Forwarded host=$host:$server_post;
	 
	      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;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

还可以修改 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;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
2.5.再次运行nginx镜像

–restart=always 表示docker重启时,容器也重启
–network=host 表示使用宿主机网络

docker stop nginx;docker rm nginx;docker run -d -it --name nginx \
 -p 7080:80 \
 -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
 -v /root/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf \
 -v /root/nginx/logs:/var/log/nginx \
 nginx:1.16.1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
2.6.docker nginx镜像 swagger未转发端口问题

  server {
    listen 80;
    server_name 10.10.10.146;
    location / {
      #default_type text/html;
      #return 200 'hello zy!';
      proxy_pass http://xxx;
      #如果swagger没有转发端口,则需要$server_port写死
      proxy_set_header Forwarded host=$host:$server_port;
      #proxy_set_header X-Forwarded-Host  $host;
      #如果是docker容器,则需要$server_port写死
      #proxy_set_header X-Forwarded-Port  $server_port; 
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/619457
推荐阅读
相关标签
  

闽ICP备14008679号