Linux下安装nginx详细教程+使用

一、进入home目录创建目录 nginx

二、准备nginx安装相关的组件

1.下载nginx:wget http://nginx.org/download/nginx-1.10.2.tar.gz

2.下载openssl:wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz

3.下载zlib:wget http://zlib.net/zlib-1.2.11.tar.gz

4.下载pcre:wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz

5.如果安装了c++的环境就可跳过,如未安装:yum install gcc-c++ (点击y即可)

安装结尾显示complete,即代表安装完成

三、以上文件准备好开始安装:

1.安装openssl

tar -zxvf openssl-fips-2.0.10.tar.gz

cd openssl-fips-2.0.10

./config && make && make install 进入文件执行安装程序

2.安装pcre

tar -zxvf pcre-8.40.tar.gz

cd pcre-8.40

./configure && make && make install

3.安装zlib

tar -zxvf zlib-1.2.11.tar.gz

cd zlib-1.2.11

./configure && make && make install

4.安装nginx:

tar zxvf nginx-1.10.2.tar.gz

cd nginx-1.10.2

./configure && make && make install

如果启动失败:error while loading shared libraries:libpcre.so.1…

[root@localhost nginx]# whereis libpcre.so.1
libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so.1 /usr/local/lib/libpcre.so
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
[root@localhost nginx]# ps -aux | grep nginx
Warning: bad syntax, perhaps a bogus ‘-’? See /usr/share/doc/procps-3.2.8/FAQ
root 12007 0.0 0.0 20296 628 ? Ss 13:28 0:00 nginx: master process sbin/nginx
nobody 12008 0.0 0.1 20716 1220 ? S 13:28 0:00 nginx: worker process
root 12010 0.0 0.0 103244 836 pts/0 S+ 13:29 0:00 grep nginx

3.安装是否成功:

添加80端口:

firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效)

重启防火墙

firewall-cmd --reload

查看80端口是否添加成功:

firewall-cmd --list-ports

功能代码
启动cd /usr/local/nginx/sbin (进入目录) ./nginx
停止./nginx -s stop
查看版本./nginx -v
查看启动状态ps -ef grep nginx
重加载./nginx -s reload

配置文件

配置文分为3部分
1.全局快:从顶端到evens块之间的内容,
2evens块:涉及影响nginx服务器与用户的链接
3http块
在这里插入图片描述

########### 每个指令必须有分号结束。#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody。
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
}

[root@centos7 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent

查询端口号80 是否开启:

[root@centos7 ~]# firewall-cmd --query-port=80/tcp

重启防火墙:

[root@centos7 ~]# firewall-cmd --reload

查询有哪些端口是开启的:

[root@centos7 ~]# firewall-cmd --list-port

命令含义:

–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效

反向代理

实现模块跳转
1.在后台启动了springboot8701 8702 8703两个项目
在这里插入图片描述

一个注意斜杠的大坑**

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

在这里插入图片描述

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    server {
        listen       80;  #监听前端访问的端口号
        server_name  172.20.10.119;#监听前端访问地址
		
		#在172.20.10.119/a  或/b 结尾分别跳转不同的地址
        location  /a {
           proxy_pass  http://127.0.0.1:8089/;
        }
		location  /b {
           proxy_pass  http://127.0.0.1:8088/;
        }
    }
}

负载均衡

设置weight权重后 根据值自动的进行分配

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
	
	upstream group1{
		server 172.20.10.119:8088  weight=2;
		server 172.20.10.119:8089  weight=1;
		
	}

    server {
        listen       80;  #监听前端访问的端口号
        server_name  172.20.10.119;#监听前端访问地址
		
		#在172.20.10.119/a  或/b 结尾分别跳转不同的地址
        location  /a {
           proxy_pass  http://group1/;
        }
    }
}
反向代理策略方式
1.轮询(默认)依次访问
2.权重weight=2 写在端口号后面
3.ip—hash当用户第一次访问的是8080的话根据ip的hash方式会一直访问8080端口便于session的传递
4.fair按照后端服务相应的时间来分配
5.backup做备份服务器,如其他服务器挂掉使用备份
6.down不参与负载均衡

1、轮询
从上到下依次访问

upstream tomcats{
     server 192.168.128.111:8080;
     server 192.168.128.111:8081;
   }

2、权重
8080访问会比8081访问次数会高(8080服务器性能好)

upstream tomcats{
     server 192.168.128.111:8080 weight=2;
     server 192.168.128.111:8081 ;
     server 192.168.128.111:8083 backup;
   }

3、ip-hash
当用户访问后,会根据ip的hash值一直请求这一个地址,来解决session共享的问题

upstream tomcats{
     ip_hash;
     server 192.168.128.111:8080;
     server 192.168.128.111:8081;
   }

ip_hash;

4、fair(第三方)
按后端服务器的相应时间来分配请求、相应时间短的优先分配。

upstream tomcats{
     ip_hash;
     server 192.168.128.111:8080;
     server 192.168.128.111:8081;
     fair;
   }

5、url_hash
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
注意:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。

upstream tomcats{
     server 192.168.128.111:8080;
     server 192.168.128.111:8081;
     hash $request_uri;
     hash_method crc32; 
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值