nginx源码安装后配置

一、静态页面配置

创建 conf.d目录

mkdir /usr/local/nginx/conf.d

vim /usr/local/nginx/conf/nginx

user nginx;
    include /usr/local/nginx/conf.d/*.conf;

接下来重启nginx

配置当网页访问时的nginx界面

vim /usr/local/nginx/html/index.html

此时配置成想要的页面效果即可

二、虚拟主机配置

1、配置(同端口不同ip)

vim /usr/local/nginx/conf.d/virtualhost.conf

server {
        listen       192.168.157.200:80;
        server_name  localhost;

        location / {
            root   /data/web1/html;
            index  index.html index.htm;
        }
}

server {
        listen       192.168.157.201:80;
        server_name  localhost;

        location / {
            root   /data/web2/html;
            index  index.html index.htm;
        }
}

创建目录 /data/web1/html 和/data/web2/html

以下两种都可以

mkdir /data/web{1,2}/html -p
vim /data/web1/html/index.html
this is web1

vim /data/web2/html/index.html
this is web2

重启nginx

systemctl start nginx

添加IP 192.168.157.200 和 192.168.157.201

 

 

2、配置(同ip不同端口)

 vim /usr/local/nginx/conf.d/virtualhost.conf

server {
        listen       192.168.157.200:80;
        server_name  localhost;

        location / {
            root   /data/web1/html;
            index  index.html index.htm;
        }
}

server {
        listen       192.168.157.200:81;
        server_name  localhost;

        location / {
            root   /data/web2/html;
            index  index.html index.htm;
        }
}

 

3、hosts解析域名访问  

 vim /usr/local/nginx/conf.d/virtualhost.conf

server {
        listen       80;
        server_name  web1.xiaoming.cn;

        location / {
            root   /data/web1/html;
            index  index.html index.htm;
        }
}

server {
        listen       81;
        server_name  web2.xiaohua.cn;

        location / {
            root   /data/web2/html;
            index  index.html index.htm;
        }
}

 访问端虚拟机配置hosts

vi /etc/hosts

 

三、日志配置

日志切割

vim /etc/logrotate.d/nginx

/usr/local/nginx/logs/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /usr/local/nginx/logs/nginx.pid ]; then
                        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
                fi
        endscript
}
执行

logrotate -f /etc/logrotate.d/nginx 

 vim /etc/logrotate.d/nginx 注释一行,添加如下一行

 此时用浏览器访问IP可以得到访问日志

四、常用功能配置

1、部署文件共享

只写一个server

vim /usr/local/nginx/conf.d/virtualhost.conf

server {
        listen       80;
        server_name  web1.xiaoming.cn;

        location / {
            root   /data/web1/html;
            index  index.html index.htm;
            autoindex on;
        }
}

 cd /data/web1/html

[root@localhost html]# mv index.html{,.bak}

创建几个压缩文件

[root@localhost html]# touch wget-1.{1..4}.tar.gz

接下来重启nginx

(这样在网页上就可以下载)

 2、状态模块

vim /usr/local/nginx/conf.d/virtualhost.conf

server {
        listen       80;
        server_name  web1.xiaoming.cn;

        location / {
            root   /data/web1/html;
            index  index.html index.htm;
            autoindex on;
        }
location /nginx_status {
    stub_status on;
    access_log off;
}
}

重启nginx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值