Linux部署nginx及nginx负载均衡的配置,保姆级教程小白必备

Linux部署nginx及nginx负载均衡的配置,保姆级教程小白必备
1、安装所需依赖

yum  -y  install pcre pcre-devel gcc openssl openssl-devel zlib zlib-devel

(pcre: 包括 perl 兼容的正则表达式库
  openssl: 支持安全传输协议https(和财务有关系的请求会走的协议)

创建运行用户、组

useradd -M -s /sbin/nologin nginx   


2、上传安装包到local下

 解压    tar   -zxvf    nginx-1.20.2.tar.gz

 切换到解压目录下
 cd /usr/local/nginx-1.20.2
 
 执行安装命令
 ./configure
  
  make&&make install

 3、切换到安装目录

 cd  /usr/local/nginx/conf

 找到nginx.conf文件,需修改端口

  vim  nginx.conf

 server {
        listen       80;(默认时80端口需要更改)
        server_name  localhost;


4、启动nginx服务
   
   cd   /usr/local/nginx/sbin
   启动命令
   ./nginx


5.查看nginx是否启动成功

ps  -ef|grep   nginx


6、设置防火墙,开放8066端口
查看防火墙是否开放过此端口
firewall-cmd --list-all  
开放端口
firewall-cmd --zone=public --add-port=8066/tcp --permanent
 一定要重启防火墙
firewall-cmd --reload

7、设置开机自动启动

切换到/lib/systemd/system/目录,创建nginx.service文件vi nginx.service

    
# cd /lib/systemd/system/
# vim nginx.service

文件内容如下:

    

[Unit]
Description=nginx 
After=network.target 
  
[Service] 
Type=forking 
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true 
  
[Install] 
WantedBy=multi-user.target

退出并保存文件,

   执行systemctl enable nginx.service   使nginx开机启动
    
   

systemctl start nginx.service    启动nginx

systemctl stop nginx.service    结束nginx

systemctl restart nginx.service    重启nginx

 ##############################nginx.conf配置文件###############################################
user  root;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;

    server {
    listen       80;
    server_name  localhost;

    location / {
        #这是nginx容器中的默认配置路径,已经映射到虚拟机/www下了 不需要改动
        root   /usr/share/nginx/html/;
        #这里添加index.php入口文件
        index index.php index.html index.htm;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    #配置php
    location ~ \.php$ {
        #这里要换成php容器的ip!
        fastcgi_pass   192.168.0.118:9000;
        fastcgi_index  index.php;
        #这一段一定要注意!把php容器中默认的/var/www/html写进去,替换掉之前的$document
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }
}

##############################轮询负载均衡配置#############################
 

   upstream lunxun_fuzai {
        server 192.168.30.128:80;
        server 192.168.30.135:80;
    }
  
    server {
        listen 80;
        server_name   192.168.1.111;
  
        location / {
            proxy_pass http:// lunxun_fuzai ;
            proxy_set_header Host $proxy_host;
        }
    }
}


 ##############################weight负载均衡配置###############################
   

 upstream order {
   ip_hash;
   server 192.168.0.118:8011 weight=1;  #weight权重
   server 192.168.0.118:8088 weight=1;
   }

 
    server{
    listen 80;
    server_name 192.168.0.118;
    location / {
       index  index.html  index.htm;
        proxy_pass http://order/;
 }
 }


####################################标准配置##############################
 

  server {
      listen   80;
     server_name  somename  alias  another.alias;
 
       location / {
            root   /usr/share/nginx/html/;
            index  index.html index.htm;
       }
    }
    
}


 

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温柔-的-女汉子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值