Linux下nginx配置、使用

目录

一、概述   

二、yum安装/rpm安装:nginx

三、nginx 配置文件修改

四、配置文件

基于不同端口号

 基于不同IP地址

基于不同域名


一、概述
    功能:web中间件、反向代理、缓存
    特点:并发连接数高,单台主机并发可到达50000
               只能解析静态页面
    工作模型:epoll
    监听端口号:80

二、yum安装/rpm安装:nginx

[root@localhost ~] ping www.baidu.com  #确保能ping通外网
PING www.baidu.com (124.237.176.4) 56(84) bytes of data.
64 bytes from 124.237.176.4 (124.237.176.4): icmp_seq=1 ttl=128 time=15.9 ms
64 bytes from 124.237.176.4 (124.237.176.4): icmp_seq=2 ttl=128 time=16.4 ms
^C
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 15.926/16.201/16.476/0.275 ms
 
[root@localhost ~] rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
获取http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm
警告:/var/tmp/rpm-tmp.riNLEC: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:nginx-1:1.14.2-1.el7_4.ngx       ################################# [100%]

三、nginx 配置文件修改

[root@localhost ~] cd /usr/share/nginx/html/
[root@localhost html] ll
总用量 8
-rw-r--r--. 1 root root 537 12月  4 2018 50x.html
-rw-r--r--. 1 root root 612 12月  4 2018 index.html

四、配置文件

路径:/etc/nginx

主配置文件:/etc/nginx/nginx.conf

主机头配置文件:/etc/nginx/con.f/default.conf

服务器启动:systemctl start nginx  停止:systemctl stop nginx

五、虚拟主机头

基于不同端口号

1.修改conf.d中主机头配置文件

[root@localhost nginx] cd conf.d/
[root@localhost conf.d] ll
总用量 4
-rw-r--r--. 1 root root 1093 12月  4 2018 default.conf
[root@localhost conf.d] cp default.conf skl1.conf   #拷贝2个虚拟主机头
[root@localhost conf.d] cp default.conf skl2.conf
[root@localhost conf.d] rm -rf default.conf     #删除原本虚拟主机头
[root@localhost conf.d] vim skl1.conf 
[root@localhost conf.d] vim skl2.conf   #同理

 server {
    listen       80;
    listen 192.168.9.143:80;  #加入你的dhcp获取的IP  第二个文件填写第二个IP
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/143.access.log  main;  #取消注释

    location / {
        root   /usr/share/nginx/html/skl1;    #改成你的index文件路径
        index  index.html index.htm;
    }

    #error_page  404              /404.html;  



 

2.注释nginx.conf中的访问日志配置项

[root@localhost nginx] vim nginx.conf

                     '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;  # 取消此行注释


3.创建访问目录并写入index.html文件

[root@localhost ~] cd /usr/share/nginx/html/
[root@localhost html] ll
总用量 8
-rw-r--r--. 1 root root 537 12月  4 2018 50x.html
-rw-r--r--. 1 root root 612 12月  4 2018 index.html
[root@localhost html] mkdir skl1
[root@localhost html] mkdir skl2
[root@localhost html] cp index.html skl1/
[root@localhost html] cp index.html skl2/
[root@localhost html] rm -rf index.html 

[root@localhost ~] cd /usr/share/nginx/html/skl1   
[root@localhost skl1] vim index.html      #写入自己的首页内容
[root@localhost skl1] vim ../
50x.html  skl1/     skl2/     
[root@localhost skl1] vim ../skl2/index.html  


4.重启服务器

[root@localhost conf.d] systemctl start nginx
[root@localhost conf.d] 

 基于不同IP地址

server {
    listen       80;
    listen 192.168.159.142:8081;  #修改
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/8081.access.log  main;  #修改

    location / {
        root   /usr/share/nginx/html/skl1;

基于不同域名

[root@localhost conf.d]# vim /etc/hosts


127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.142    dns.skl.com      #添加域名声明
192.168.159.145    www.skl.com      #添加域名声明
~                                                                                                                                                                                                                  
~                                                    

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我还能再学点

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

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

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

打赏作者

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

抵扣说明:

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

余额充值