nginx的配置

nginx介绍

一款高性能,轻量级web服务软件

1.稳定性高

2.系统资源消耗低

        对http并发连接的处理能力高,对静态页面的处理比较好,但是对动态页面的支持效果不佳

源码包安装

在开启nginx之前,首先要确定80端口没有被httpd的服务给占用,否则会报错

nginx: [emerg] bind() to 192.168.152.100:80 failed (98: Address already in use)
nginx: [emerg] bind() to 192.168.152.100:80 failed (98: Address already in use)
nginx: [emerg] bind() to 192.168.152.100:80 failed (98: Address already in use)
nginx: [emerg] bind() to 192.168.152.100:80 failed (98: Address already in use)
nginx: [emerg] bind() to 192.168.152.100:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

首先找到源码包并上传到服务器

安装nginx对应的依赖包

yum -y install zlib-devel pcre-devel

再进行解压安装

useradd -M -s /sbin/nologin nginx    创建一个用户

tar -zvxf nginx-1.12.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.12.0/

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
##--with-http_stub_status_module启用统计模块

进行启动命令的优化

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

没有图形画的用户安装elinks可以实现nginx的访问

yum -y install elinks

主配置文件的修改

http{        设置的是全局配置
events {
    worker_connections  1024;    能够支持的连接数
}

    server {
        listen       80;            侦听的端口,可以直接是端口,也可以是IP地址+不同端口
        ##listen 192.168.152.100:8080

        server_name  www.ym.com;    可以是公司的域名,IP
        server_name 192.168.152.100:8080

        access_log  logs/www.ym.com.access.log ;     访问日志
        sendfile on;                                 启用高效传输
        keepalive timeout 65;                       设置超时时间
       
        location / {
            root   html/ym;                          网页根目录
            index  index.html index.htm;             首页文件
            #deny 192.168.152.100;                   拒绝这个主机名称
            #allow all;                              允许其他所有
        }
        location /status{
            stub_status on;
            access_log off;
        }                                            启用统计模块
        error_page   500 502 503 504  /50x.html;     错误页面
        location = /50x.html {                       错误日志的位置
            root   html;
        }

server{}之内的东西是一个虚拟主机

编辑首页文件

cd /usr/local/nginx/html
mkdir ym        网页的首页名称
echo "内容" > ym/index.html

启动

nginx

nestart -nultp|grep nginx

配置完成dns进行解析域名,如果没有域名的话可以直接访问IP地址

进行访问

elinks http://网页名称
如elinks http://192.168.152.100
  elinks http://www.ym.com

如果使用用户密码登录,可以进行以下的配置

在当前文件下生成密码文件

[root@localhost html]# pwd
/usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# htpasswd -c ./nginxpwd mc
New password: 
Re-type new password: 
Adding password for user mc
[root@localhost html]# htpasswd  ./nginxpwd ym
New password: 
Re-type new password: 
Adding password for user ym

修改主配置文件

location / {
    root html/ym;
    index index.html index.html;
在网页根目录的下面进行配置
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/html/nginxpwd;
}

以上是nginx主配置文件的基本配置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值