nginx配置

Posted by 小兵兵 on Thursday, March 12, 2020

目录

nginx配置

参考

防火墙

开启httpssh服务

开放80、81、82、83、8080、8081端口

[zbiti@hadoop105 sbin]$ sudo firewall-cmd --add-service=http --permanent
success
[zbiti@hadoop105 sbin]$ sudo firewall-cmd --add-port=81/tcp --permanent
success
[zbiti@hadoop105 sbin]$ sudo firewall-cmd --reload
success
[zbiti@hadoop105 sbin]$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client http ssh
  ports: 80/tcp 8080/tcp 8081/tcp 81/tcp 82/tcp 83/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[zbiti@hadoop105 sbin]$ 

安装

反向代理

修改win10机子上的hosts文件

192.168.1.105  www.123.com

修改nginx配置文件,然后重启

#配置反向代理一
#访问www.123.com
    server {
        listen       80;
        server_name  192.168.1.105;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
			proxy_pass	http://192.168.1.105:8080;
            index  index.html index.htm;
        }

        #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;
        #}
    }
    
    #配置反向代理二
    #访问http://192.168.1.105:81/edu/a.html
     #访问http://192.168.1.105:81/vod/a.html
	server {
        listen       81;
        server_name  192.168.1.105;

        location / {
            root   html;
			proxy_pass	http://192.168.1.105:8080;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		
		location ~ /edu/ {
            proxy_pass   http://127.0.0.1:8080;
        }
		location ~ /vod/ {
            proxy_pass   http://127.0.0.1:8081;
        }
       
    }

负载均衡

修改nginx配置文件,然后重启

#配置负载均衡使用ip hash 
#访问http://192.168.1.105:82/edu/a.html
	upstream tomcats {
		ip_hash;
        server 192.168.1.105:8080;
        server 192.168.1.105:8081;
    }
	
	server {
        listen       82;
        server_name  192.168.1.105;

        location / {
            root   html;
			proxy_pass	http://tomcats;
            index  index.html index.htm;
        }
		
    }

动静分离

修改nginx配置文件,然后重启

#配置动静分离 访问http://192.168.1.105:83/index.html
	#http://192.168.1.105:83/business/
	server {
        listen       83;
        server_name  192.168.1.105;

        location / {
            root   /opt/module/public/;
            index  index.html index.htm;
        }
		
		location /business/ {
            root   /opt/module/;
            index  index.html index.htm;
        }
		
    }

高可用keepalived

开启gzip压缩

修改nginx配置文件

    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

开启前

开启后

curl测试

[root@izm5eac6bnsz8uq175jkvez ~]# curl -I -H "Accept-Encoding: gzip, deflate" "http://bingbing.tech/"
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 19 Mar 2020 12:59:50 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 19052
Last-Modified: Wed, 18 Mar 2020 14:59:16 GMT
Connection: keep-alive
ETag: "5e723744-4a6c"
Accept-Ranges: bytes

[root@izm5eac6bnsz8uq175jkvez ~]# curl -I -H "Accept-Encoding: gzip, deflate" "http://bingbing.tech/"
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 19 Mar 2020 13:02:54 GMT
Content-Type: text/html; charset=utf-8
Last-Modified: Wed, 18 Mar 2020 14:59:16 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"5e723744-4a6c"
Content-Encoding: gzip

参考

Nginx性能优化功能- Gzip压缩(大幅度提高页面加载速度)

nginx开启gzip压缩的配置详解和压缩效果对比