nginx学习

nginx安装

转载于:https://blog.csdn.net/weixin_30898109/article/details/97590536

系统基础环境:
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
# cat /proc/version
Linux version 3.10.0-957.5.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Fri Feb 1 14:54:57 UTC 2019
 

安装方法一:源码编译安装
基础环境 : CentOS Linux release 7.6.1810 (Core)

目录:/usr/local/src

 

1.1、安装前准备(根据各软件最新版本信息更改url)
下载nginx、openssl、zlib、pcre

# cd /usr/local/src

# wget http://nginx.org/download/nginx-1.16.0.tar.gz
# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
# wget http://zlib.net/zlib-1.2.11.tar.gz
# wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
 

1.2、安装(顺序不要错)

安装make

# yum -y install gcc automake autoconf libtool make
 

安装gcc编译环境

# yum install gcc-c++
 

安装pcre

# tar zxvf pcre-8.43.tar.gz
# cd pcre-8.43
# ./configure && make && make install
 

安装openssl:

# cd ..
# tar zxvf openssl-fips-2.0.10.tar.gz
# cd openssl-fips-2.0.10
# ./config && make && make install
 

安装zlib

# cd ..
# tar zxvf zlib-1.2.11.tar.gz
# cd zlib-1.2.11
# ./configure && make && make install
 

安装nginx

# cd ..
# tar zxvf nginx-1.16.0.tar.gz
# cd nginx-1.16.0
# ./configure && make && make install
 

这样安装完成后,nginx的路径为/usr/local/nginx/sbin/nginx

# whereis nginx

nginx: /usr/local/nginx
 

安装方法二:
查看CentOS的版本

# cat /etc/redhat-release
  CentOS Linux release 7.6.1810 (Core)

 

添加资源库 , 在 CentOS 系统上安装 Nginx ,得先去添加一个资源库:

# vim /etc/yum.repos.d/nginx.repo
 

输入如下代码:

  [nginx]
  name=nginx repo
  baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  gpgcheck=0
  enabled=1
 

安装nginx

# yum -y install nginx
 

使用命令
# systemctl stop nginx.service  #停止nginx服务
# systemctl start nginx.service  #打开nginx服务
# systemctl restart nginx.service  #重启nginx服务
# systemctl status nginx.service  #查看nginx服务状态
 

开启并验证nginx安装成果:
开启nginx:
# cd /usr/local/nginx/sbin
# ./nginx# systemctl start nginx.service
验证:
# curl http://localhost
 

应该能输出Welcome to nginx的html文件,说明安装启动成功。

 
开启外部访问
centos7默认开启了防火墙模块firewalld,可以查看一下是否开启

# ystemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-03-18 10:12:46 EDT; 34s ago
     Docs: man:firewalld(1)
 Main PID: 3618 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─3618 /usr/bin/python -Es /usr/sbin/firewalld --nofork...

Mar 18 10:12:46 localhost.localdomain systemd[1]: Starting firewal...
Mar 18 10:12:46 localhost.localdomain systemd[1]: Started firewall...
Hint: Some lines were ellipsized, use -l to show in full.

或者:

# firewall-cmd --state
running
  

关闭firewalld,然后在虚拟机外可以访问

# systemctl stop firewalld #关闭
# systemctl start firewalld #打开
# systemctl restart firewalld #重启
# systemctl disable firewalld #关闭开机启动
# systemctl enable firewalld #开机启动
 
外部访问nginx主页:http://***.***.***.***
**

## 添加 nginx 为系统服务,设置开机自启

**
1、在 /lib/systemd/system 目录添加 nginx.service 文件

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

2、写入以下内容,保存退出

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s  stop
ExecRestart=/usr/local/nginx/sbin/nginx -s  restart
PrivateTmp=true
   
[Install]  
WantedBy=multi-user.target
 

3、设置开机自启

# systemctl enable nginx
 

4、其它命令

# 启动 nginx
systemctl start nginx

# 停止
systemctl stop nginx

# 加载配置文件
systemctl reload nginx

# 重启
systemctl restart nginx

# 查看状态
systemctl status nginx

网上视频补习了下nginx,有些字段含义不明,针对配置文件中的字段学习下:
配置文件的字段
参考文档:https://www.cnblogs.com/regit/p/9302568.html
1.root 相对路径
最终实际访问路径:Parentparh+/data/www/index.html

server{
         listen 80;
         server_name www.wzw.com;

         location /www {
                 root /data/;  //设置虚拟主机主目录相对路径
                 index  index.html;  //设置虚拟主机默认主页
         }
}

2.alias 绝对路径
最终实际访问路径:/var/www/image/index.html

server{
                listen 80;
                server_name www.wzw.com;

                location /img/ {
                        alias /var/www/image/;
                        index    index.html;     
                }
}

项目应用

1.反向代理
医院内网环境,需要访问支付系统,给医院his系统部署一台nginx服务器做反向代理
2.动静分离
前端页面,静态文件代理
admin shop 分销系统管理前端文件代理
3.负载均衡
腾讯云也使用负载均衡。
负载均衡格式:
upstream 负载名称{
server ip:port;
}
负载均衡+反向代理举例配置


  upstream myserver{
       server 119.24.243.55:8081;
  }
   server {
        listen       8080;
        server_name  localhost;
        location ~ /api/pay/ {
            proxy_pass  http://myserver;
        }

       }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值