linux安装Nginx
1.安装依赖
//一键安装上面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2.下载解压安装
//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载tar包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz -C /usr/local/nginx
//使用nginx默认配置
3.安装Nginx
//进入nginx目录
cd /usr/local/nginx
//进入目录
cd nginx-1.13.7
//执行命令,使用nginx默认配置 考虑到后续安装ssl证书 添加两个模块
./configure --with-http_stub_status_module --with-http_ssl_module
//执行make命令
make
//执行make install命令
make install
4.常用命令
[root@VM-8-16-centos sbin]# ./nginx -v //查看版本
[root@VM-8-16-centos sbin]# ./nginx -t //启动前检查配置是否正确
[root@VM-8-16-centos sbin]# ./nginx //启动
[root@VM-8-16-centos sbin]# ./nginx -s stop //停止
[root@VM-8-16-centos sbin]# ./nginx -s reload //重新加载
5.配置环境可以不用进入nginx目录下命令
vim /etc/pofile
export PATH=$PATH:$JAVA_HOME/bin:/usr/local/nginx-1.20.2/sbin
source /etc/profile
nginx配置文件结构
##代码块中的events、http、server、location、upstream等都是块配置项##
##全流程:比如我们访问10.10.10.999:8080/myproject/ 这个地址,10.10.10.999:8080就是我们的nginx主机地址以及server中设置的端口号。然后myproject根据location中配置的规则进行匹配,匹配到了就用那条location里proxypass的值去找符合的upstream(upstream设置了负载均衡规则,包含哪些被代里的主机地址等)
#规定哪些用户使用,nobody的话就是所有人
#user nobody;
##Nginx worker进程个数:其数量直接影响性能。
#每个worker进程都是单线程的进程,他们会调用各个模块以实现多种多样的功能。如果这些模块不会出现阻塞式的调用,那么,有多少CPU内核就应该配置多少个进程,反之,有可能出现阻塞式调用,那么,需要配置稍多一些的worker进程。
worker_processes 1;
events {
worker_connections 1024;
}
http {
#引入mime.types文件的设置,这样引入的方式主要是增加可读性
include mime.types;
default_type application/octet-stream;
sendfile on;
#设置一个tcp连接最多保持多长时间存活
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
#location主要作用是根据请求地址路径的匹配,匹配成功进行特定的处理
#跳转的nginx首页,首页文件在根目录的html文件夹下的index.html
location / {
root html;
index index.html;
}
#出错页面,出错的话就会跳转。在根目录的html文件夹下的/50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#负载均衡的策略在upstream块这里配置,如下是轮询的一个范例
upstream bakend {
server 192.168.1.10 weight=1;
server 192.168.1.11 weight=2;
}
#引入其他的conf
include imooc.conf;
}
----------------------------------------------------------------------------------
server {
listen 81;
server_name localhost;
//跳转的nginx首页,首页文件在根目录的html文件夹下的index.html
location / {
root html;
index imooc123.html;
}
}
具体应用
1.部署静态资源
2.反向代理
3.负载均衡
部署静态资源
配置文件中有一个location的配置,配置静态资源,主要就是用它
例如:
比如现在静态资源的总目录api,在linux中的绝对路径是 /software/api,那么location就应该这么配置
location /api {
root /software;
index login.html;
}
浏览器中输入http://192.168.1.1(:80)/api,就会被这个location匹配,跳转到/software/ace/login.html。也就是说root + location一块拼成了index的访问地址
// location只有一个/ ,所以最后index.html的访问路径就是/usr/local/nginx/html/index.html
location / {
root html;
index index.html,index.htm; //默认首页,第一个找不到,找第二个 ps:末尾有分号
}
反向代理
客户端—>反向代理服务器(192.168.1.1)---->web服务器(192.168.1.2)
配置如下
server {
listen 80;
server_name localhost //www.baidu.com;
location / {
proxy_pass http://192.168.1.2:8080; //www.guge.com
}
}
负载均衡
upstream tong_server { //配置负载均衡服务
server 192.168.202.131:8080 weight=2; //配置第一个节点权重为2
server 192.168.202.1328080 weight=1; //配置第二个节点权重为1
}
server {
listen 8080; //对外监听端口
server_name localhost; //对外提供服务的IP地址
location / {
proxy_pass http://tong_server; //调用的负载均衡服务
}
}