快速安装Nginx及配置详解(未完待续)

导读:

Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,从2007年被德国人开发出来后可以说在市场的占有率一路飙升,因为它支持高并发,而且还能阻止dos攻击,它是当前较具影响力的一个http服务器软件,像百度等大厂都使用它,所以这是作为一个运维人必须学会的软件。

安装

配置好yum源:

 1 [root@network yum.repos.d]# cat aliyun.repo 
 2 [rhel7]
 3 name=ali base
 4 baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
 5 enabled=1
 6 gpgcheck=0
 7 
 8 #阿里云epel
 9 [epel]
10 name=ali epel
11 baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
12 enabled=1
13 gpgcheck=0
14 
15 #阿里云extras
16 [extras]
17 name=ali extras for centos 7
18 baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
19 enabled=1
20 gpgcheck=0
1 yum -y install nginx

直接启动就可以使用了

咱们就先来试下

1 systemctl start nginx

查看下端口

1 [root@network ~]# netstat -ntlp | grep nginx
2 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      51970/nginx: master 
3 tcp6       0      0 :::80                   :::*                    LISTEN      51970/nginx: master 

使用elinks访问一下,或者你也可以用图形化界面的浏览器去访问

1 [root@network ~]# elinks http://192.168.10.101

主要文件:

1 配置目录      /etc/nginx
2 主程序        /usr/sbin/nginx
3 启动脚本      /usr/lib/systemd/system/nginx.service
4 默认站点目录  /usr/share/nginx/html
5 日志目录      /var/log/nginx

再了解下主配置目录下的目录结构

 1 [root@network ~]# tree /etc/nginx/
 2 /etc/nginx/                    # 主进程目录
 3 ├── conf.d                     # 子进程目录
 4 │   ├── default.conf           # 虚拟主机模板配置文件
 5 │   └── example_ssl.conf       # https虚拟主机模板配置文件
 6 ├── fastcgi_params             # 程序变量
 7 ├── koi-utf
 8 ├── koi-win
 9 ├── mime.types                 # 文件扩展名与文件类型映射表
10 ├── nginx.conf                 # 主配置文件
11 ├── scgi_params
12 ├── uwsgi_params
13 └── win-utf

其他文件就不介绍了,因为我们也很少会用到,挑重点给大家

 基本配置

 为了方便了解主配置文件的内容,我们查看并过滤掉空行来了解

 1 [root@network ~]# cat /etc/nginx/nginx.conf | grep -v "^$"
 2 user  nginx;                # 所有子进程所使用用户身份/etc/passwd里查看
 3 worker_processes  1;        # 开机Nginx的进程数
 4 error_log  /var/log/nginx/error.log warn;    # 错误日志
 5 pid        /var/run/nginx.pid;               # 记录进程PID号的文件
 6 events {
 7     worker_connections  1024;     # 每个进程最大链接数,可以是65535
 8 }
 9 http {
10     include       /etc/nginx/mime.types;    # 文件扩展名与文件类型映射表
11     default_type  application/octet-stream; # 默认文件类型
12     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '       # 定义maim日志记录类型
13                       '$status $body_bytes_sent "$http_referer" '
14                       '"$http_user_agent" "$http_x_forwarded_for"';
15     access_log  /var/log/nginx/access.log  main;   # 访问日志
16     sendfile        on;     # 搞笑文件传输模式
17     #tcp_nopush     on;     # 配置一次发送数据的包大小,必须和sendfile一起使用
18     keepalive_timeout  65;  # 保持链接超时时间
19     #gzip  on;              # 压缩
20     include /etc/nginx/conf.d/*.conf;   # 读取目录下以.conf的文件,加载进来
21 }

一:进程管理

1 [root@network ~]# cat /etc/nginx/nginx.conf | grep -v "^$"
2 user  nginx;
3 worker_processes  1;
4 error_log  /var/log/nginx/error.log warn;
5 pid        /var/run/nginx.pid;
6 events {
7     worker_connections  1024;
8 }

就这段,进程管理我们只要看两个数值就可以

进程:worker_processes 1;

这个跟你主机的CPU有关,是与CPU的核心数成倍数的,假如你的CPU核心数是双核,那就配置4进程,如果四核就是配置为8进程,同理

线程:worker_connections 1024;

这个是线程,是单个进程所允许打开的线程数量,最高可以配置65535,但还是得根据你操作系统设置的最大文件打开数量有关

1 # 查看当前文件打开数量值
2 [root@network ~]# ulimit -n
3 1024
4 # 设置为65535
5 [root@network ~]# ulimit -HSn 65535
6 [root@network ~]# ulimit -n
7 65535

如果你服务器系统默认设置的是1024,而你nginx设置为65535,启动时不会报错,但在你执行检查语法的时候就会报以下错误

1 [root@network ~]# nginx -t
2 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3 nginx: configuration file /etc/nginx/nginx.conf test is successful

二:虚拟主机配置

讲完进程,我们就来配置下虚拟主机,学过Apache就知道它可以配置很多个虚拟主机,且可以通过不通的域名访问到不通的虚拟主机,所以我们也来配置下Nginx的虚拟主机。

先进去到子进程配置文件里,我们拷贝模板文件后来进行修改。
站点的域名就使用www.zhou.com

1 [root@network ~]# cd /etc/nginx/
2 [root@network nginx]# ls
3 conf.d                fastcgi_params          mime.types          scgi_params           win-utf
4 default.d             fastcgi_params.default  mime.types.default  scgi_params.default
5 fastcgi.conf          koi-utf                 nginx.conf          uwsgi_params
6 fastcgi.conf.default  koi-win                 nginx.conf.default  uwsgi_params.default
7 [root@network nginx]# vim nginx.conf

把配置文件里不重要的东西我们先给它删了,剩下我们要的配置就可以

 

修改完创建一个主页文件index.html

1 [root@network nginx]# echo "hello nginx" >/usr/share/nginx/html/index.html

重启nginx服务

1 [root@network nginx]# systemctl restart nginx

更改hosts文件

1 echo '192.168.100.101 www.zhou.com' >>/etc/hosts

猜你喜欢

转载自www.cnblogs.com/zhoul/p/9925383.html