3种安装nginx的方法以及相关的配置文件

#Nginx采用Epool网络模型,Apache采用Select模型

Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。(#起点不一样)

HTTP(超文本传输协议)或 HTTPS(安全超文本传输协议)

#静态WEB软件,静态请求不调用数据库,直接访问nfs共享存储服务器
nginx
apache
IIS
lighttpd
tengine (淘宝)
openresty-nginx

#动态WEB软件,动态请求调用缓存,数据库
Tomcat
Resin
weblogic
Jboss
#无论是静态请求还是动态请求都不是直接访问数据库的,而且是通过代码连接的

源码nginx安装的时候有些模块会自动下载安装,有些模块需要指定才会下载,开发指定的模块,我来下载

配置yum仓库的时候不要最好不要用中文,和大写字母

#VPN属于远程访问技术,C/S结构
简单地说就是利用公用网络架设专用网络
让外地员工访问到内网资源,利用VPN的解决方法就是在内网中架设一台VPN服务器。
外地员工在当地连上互联网后,通过互联网连接VPN服务器,然后通过VPN服务器进入
企业内网。为了保证数据安全,VPN服务器和客户机之间的通讯数据都进行了加密处理。
有了数据加密,就可以认为数据是在一条专用的数据链路上进行安全传输,
就如同专门架设了一个专用网络一样,但实际上VPN使用的是互联网上的公用链路,因此VPN称为虚拟专用网络

nginx 安装方式

1.阿里云的epel仓库
	版本较低,安装简单(yum),配置不易读
2.源码编译安装(wget)
	版本随意,安装相对来说复杂一点,升级,容易管理
3.曾导仓库,做好的rpm包
	http://test.driverzeng.com/
	
  自定义yum仓库,然后使用yum下载,使用yum下载既可以下载阿里云nginx,又可以下载官网nginx,#优先使用官网的仓库去下载
    
#nginx主页上可以下载源码包(源代码),此外还可以下载yum包

非源码安装

1.不用安装nginx依赖
yum install -y gcc gcc-c++ glibc zlib-devel pcre-devel openssl-devel

2.下载阿里云非源码nginx
yum install -y nginx

3.非源码nginx的启动,# 使用一种方式启动,就用这种方式来管理,不要混用
/usr/sbin/nginx
systemctl start nginx

4.非源码nginx的停止
/usr/sbin/nginx -s stop
systemctl stop nginx

5.非源码nginx的重启
systemctl restart nginx

6.非源码nginx重新加载配置文件
/usr/sbin/nginx -s reload
systemctl reload nginx
systemctl restart nginx

7.加入开机自启动
systemctl enable nginx	 
#无论是源码安装还是非源码安装的nginx都可以使用该方式,让nginx加入开机自启动

8.检测nginx是否安装成功
netstat -lntup |grep 80
ps -ef|grep [n]ginx

8.检查nginx版本和编译参数,使用yum安装nginx,自动加入了环境变量,可以直接使用systemctl来管理
nginx -v
nginx -V

非源码安装配置文件

[root@web02 ~]# rpm -q nginx
nginx-1.16.1-1.el7.x86_64
[root@web02 ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/nginx.conf	#
/etc/nginx/conf.d		#

[root@web02 ~]# rpm -ql nginx
/etc/logrotate.d/nginx	
#nginx默认的控制日志切割的文件(默认每天切割一次),可以对nginx,apache,yum进行日志切割,是系统自带的功能,在该目录下,写入脚本,可以切割指定的内容
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params					#Fastcgi代理配置文件
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf				#Nginx编码转换映射文件
/etc/nginx/koi-win				#Nginx编码转换映射文件
/etc/nginx/mime.types	#Content-Type与扩展名(该文件记录着在该网站可以直接打开的文件类型,文件中不存在的就要下载,用后缀来区分类型,在浏览器可以看到一一对应的关系)
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf	#nginx主配置文件
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params						#scgi代理配置文件
/etc/nginx/scgi_params.default		#
/etc/nginx/win-utf				#Nginx编码转换映射文件
/usr/sbin/nginx		#Nginx命令行管理终端工具(在环境变量里面)
/var/log/nginx		#nginx默认存放日志的目录(access,error)
/usr/bin/nginx-upgrade #Nginx命令行与终端调试工具(在环境变量里面)

/usr/share/nginx/html/index.html	#源码安装nginx的默认页面(可以修改,复制粘贴),如果想要修改nginx的默认页面的话:
1.修改index.html代码
2.在/etc/nginx/conf.d/syy.conf 里面添加一个server语句(删除default.conf ),修改root的指向就好了



[root@web02 ~]# ll /etc/nginx/conf.d/default.conf	#默认网站配置文件,这是从官网下载安装的软件包里面才有的这个软件,阿里云nignx包就没有这个文件,这个文件(废物)
[root@web02 ~]# ll /etc/nginx/scgi_params			#scgi代理配置文件
-rw-r--r-- 1 root root 636 Oct  3  2019 /etc/nginx/scgi_params
[root@web02 ~]# ll /etc/nginx/uwsgi_params			#uwsgi代理配置文件
-rw-r--r-- 1 root root 664 Oct  3  2019 /etc/nginx/uwsgi_params
[root@web02 ~]# ll /usr/sbin/nginx-debug			#Nginx命令行与终端调试工具

[root@web02 ~]# ll /usr/lib/systemd/system/nginx.service   #可以使用systemctl管理
-rw-r--r-- 1 root root 618 Oct  3  2019 /usr/lib/systemd/system/nginx.service

[root@web02 ~]# which nginx		#可以TAB和使用相对路径执行
/usr/sbin/nginx

[root@web01 /etc/nginx]# ll /var/log/nginx/
total 8
-rw-r--r-- 1 root root 1429 May 14 19:35 access.log		#nginx访问日志
-rw-r--r-- 1 root root  642 May 14 19:20 error.log		#nginx的报错日志

[root@web01 /etc/nginx]# cat /var/log/nginx/access.log |wc -l	#统计nginx的总pv

源码仓库安装(wget)

1.安装nginx依赖
yum install -y gcc gcc-c++ glibc zlib-devel pcre-devel openssl-devel

2.#考虑到网速原因,使用第三方站点下载源码nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
或者,windows从官网下载,再
rz (较慢)

3.解压,生成,编译,安装,#可能需要禁用某些模块
tar xf nginx-1.18.0.tar.gz && cd nginx-1.18.0
useradd nginx -s /sbin/nologin -M
mkdir /app && 
./configure --prefix=/app/nginx-1.18.0 --user=nginx --group=nginx
make && make install

ln -s /app/nginx-1.18.0 /app/nginx

4.#启动源码nginx
/app/nginx/sbin/nginx

设置开机自启动(选)
systemctl enable nginx

5.查看是否启动成功
	1>端口检测,netstat -lntup
	2>进程检测,ps -ef|grep nginx
	3>浏览器输入IP检测(默认界面)

源码安装nginx的配置文件

[root@web01 /app/nginx]# ll
total 0
drwxr-xr-x 2 root  root 333 May 14 07:16 conf
drwxr-xr-x 2 root  root  40 May 14 07:16 html
drwxr-xr-x 2 root  root  58 May 14 07:20 logs
drwx------ 2 nginx root   6 May 14 07:20 proxy_temp
drwxr-xr-x 2 root  root  19 May 14 07:16 sbin

源码仓库安装(yum)


1.复制粘贴官网仓库,或者自己手写,配置yum仓库的时候不要最好不要用中文,和大写字母
[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2.安装nginx
[root@web01 ~]# gzip /etc/yum.repos.d/epel.repo 
yum install nginx -y

---------------------------------------------
#下载自己制作的nginx rpm包
1.wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.0-1.el7.ngx.x86_64.rpm

2.yum localinstall -y nginx

[root@web01 ~]# rpm -q nginx
nginx-1.18.0-1.el7.ngx.x86_64
[root@web01 ~]# rpm -qc nginx
/etc/logrotate.d/nginx			#Nginx默认的日志切割
/etc/nginx/conf.d/default.conf	 #默认网站配置文件(忘记他)
...

nginx相关命令

使用systemctl启动nginx,那么就只能用systemctl来关闭nginx

使用绝对路径(或者是相对路径)启动nginx,就只能使用绝对路径(或者是相对路径)来关闭nginx

使用yum安装的话,直接就使用systemctl来管理得了

1.启动源码nginx
/usr/sbin/nginx			 
nginx				#属于源码启动,使用该命令了之后,不能再使用systemctl来管理		
systemctl start nginx	 

2.关闭nginx
/usr/sbin/nginx -s stop
nginx -s stop
systemctl stop nginx

3.重启nginx
/usr/sbin/nginx	-s restart   #不支持该格式

/usr/sbin/nginx -s stop && /usr/sbin/nginx
nginx -s stop && nginx
systemctl restart nginx

4.重新加载nginx配置文件
/usr/sbin/nginx -s reload
systemctl reload nginx	
#优先使用,重新加载配置文件,不影响客户的正常访问(配置文件出错了不会加重载),使用		 ps -ef|grep nginx 可以看到nginx子进程pid发生改变
systemctl restart nginx	 
#这种方法的缺点就是,如果配置文件出错的话,nginx就不能启动,会影响客户的访问

5.查看nginx的软件版本和编译参数
nginx -v
nginx -V

6.检查语法
/usr/sbin/nginx -t
nginx -t

一种方法的启动,另一种方法管理不了

官方nginx包nginx.conf文件

官方包,nginx主配置文件是优化后的,只有主配置内容,没有任何一个server

#-----------------------------------------------------核心模块
#nginx的启动用户
user  nginx;
#nginx worker进程数
worker_processes  10;
#nginx的报错日志的路径和级别
error_log  /var/log/nginx/error.log warn;
#pid文件的路径,Nginx服务运行后产生的pid进程号
pid        /var/run/nginx.pid;

#-----------------------------------------------------事件驱动模块
events {
#每一个worker进程允许连接数量(可以认为是用户的请求)
    worker_connections  1024;
#事件驱动模型, epoll默认    
#    use epoll;     
}

#-----------------------------------------------------http模块
#http层开始
http {
#include 包含指定文件的内容,该文件记录着nginx搭载网站允许访问的文件类型
    include       /etc/nginx/mime.types;
#默认类型,浏览器上要下载的类型(前提是该资源在mime.types中无法找到)
    default_type  application/octet-stream;
#日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

#日志路径和指定格式
    access_log  /var/log/nginx/access.log  main;

#高效传输文件
    sendfile        on;
    #tcp_nopush     on;

#长链接的超时时间
    keepalive_timeout  65;

#nginx网站是否开启gzip压缩(跟访问速度有关)
    #gzip  on;

#包含下面所有路径下以.conf结尾的文件
    include /etc/nginx/conf.d/*.conf;                                       

阿里云nginx包nginx.conf文件

[root@web02 ~]# vim /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

#这些server就是网站,指定了端口,域名或者IP,站点目录,index,通过配置不同的server可以自定义网站的端口,域名或者IP,站点目录,index,可以在这里添加server,也可以在conf.d目录下添加server,使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
#http结束层

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}


总结

一个nginx服务器可以做多个网站,通过在/etc/nginx/conf.d目录下添加server 语句来完成

官方nginx修改站点目录

使用官方nginx,同一主机创建3个不同域名的网站

官方nginx主配置文件(nginx.conf)里面一个server都没有,因为server都被放到了指定的文件中(conf.d),为了方便管理

自己可以把server语句加入到nginx.conf文件中,但是要注意加入的位置

1.手写server语句
[root@web01 ~]# vim /etc/nginx/conf.d/syy1.conf 
server {
        listen 80;
        server_name www.syy1.com;
        root /code/syy1;
        index index.html;
}
[root@web01 ~]# vim /etc/nginx/conf.d/syy2.conf
server {
        listen 80;
        server_name www.syy2.com;
        root /code/syy2;
        index index.html;
}
[root@web01 ~]# vim /etc/nginx/conf.d/syy3.conf
server {
        listen 80;
        server_name www.syy3.com;
        root /code/syy3;
        index index.html;
}

2.重启nginx
systemctl restart nginx
systemctl reload nginx
/usr/sbin/nginx -s reload

3.创建站点目录
[root@web01 ~]# mkdir /code/syy1{1..3} -p

4.创建index.html文件,并写入内容
[root@web01 ~]# echo syy1_page >/code/syy1/index.html
[root@web01 ~]# echo syy2_page >/code/syy2/index.html
[root@web01 ~]# echo syy3_page >/code/syy3/index.html

5.配置本地域名解析,win+R,输入drivers,编辑本地hosts文件
10.0.0.7 www.syy1.com
10.0.0.7 www.syy2.com
10.0.0.7 www.syy3.com

6.浏览器访问 10.0.0.7 或者www.syy1.com ,www.syy2.com ,www.syy3.com

阿里云nginx修改站点目录

使用阿里nginx,同一主机创建3个不同域名的网站

1.修改阿里云nginx的主配置文件,添加server语句
[root@web02 ~]# vim /etc/nginx/nginx.conf
...
#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
#


server {
        listen 80;
        server_name www.syya.com;
        root /code/syy1;
        index index.html;
}
server {
        listen 80;
        server_name www.syyb.com;
        root /code/syy2;
        index index.html;
}
server {
        listen 80;
        server_name www.syyc.com;
        root /code/syy3;
        index index.html;
}

2.重启nginx
systemctl restart nginx
systemctl reload nginx
/usr/sbin/nginx -s reload

3.创建站点目录
[root@web02 ~]# mkdir /code/syy1{1..3} -p

4.创建index.html文件,并写入内容
[root@web02 ~]# echo a >/code/syy1/index.html
[root@web02 ~]# echo b >/code/syy2/index.html
[root@web02 ~]# echo c >/code/syy3/index.html

5.配置本地域名解析,win+R,输入drivers,编辑本地hosts文件
10.0.0.8 www.syya.com www.syyb.com www.syyc.com

6.浏览器访问 10.0.0.8 或者www.syya.com ,www.syyb.com ,www.syyc.com

使用IP访问,只显示第一个server指定的站点目录下的index.html里面的内容
原文地址:https://www.cnblogs.com/syy1757528181/p/12891348.html