Nginx 概述
Nginx 是一款高性能的 http web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。,特点是占有内存少,并发能力强,事实上 nginx 的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用 nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等 。
官方测试 nginx 能够支支撑 5 万并发链接,并且 cpu、内存等资源消耗却非常低,运行非常稳定。
简介: 本地虚拟机环境介绍和注意事项
- 虚拟机系统版本要求:CentOS 7以上版本 64位 的镜像
- ⽹络访问:需要宿主机和虚拟机⽹络可以访问,防⽕墙关闭,或者开放80端⼝
-
镜像安装及网络配置可参考文章:https://www.cdsy.xyz/computer/soft/others/231205/cd46689.html
Nginx 安装
官网下载或 直接从我分享的网盘资源下载
链接:https://pan.baidu.com/s/1OqYfa5aX_iC1GHKkdef38w
提取码:yyds
将下载的安装包上传到linux服务器的soft目录下(soft是本人专门创建的用于存储上传的tar压缩包目录)
- yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
-
将nginx安装到/usr/local/目录下
- tar -zxvf nginx-1.18.0.tar.gz -C /usr/local/
-
执⾏命令
将安装目录下执行以下命令:
- ./configure
-
编译安装
继续执行命令:
- make
-
继续执行命令:
- make install
-
执行以上命令完成之后,nginx默认会安装到路径:/usr/local/nginx
nginx相关的启动命令,就在这个目录下sbin目录下
访问配置
- cd /usr/local/nginx/sbin
-
启动nginx
- ./nginx
-
查看配置文件当中nginx的默认端口
/usr/local/nginx/conf 目录下的nginx.conf文件
nginx 安装目录下,其默认的配置文件都放在这个目录的 conf 目录下,而主配置文件nginx.conf 也在其中,后续对 nginx 的使用基本上都是对此配置文件进行相应的修改
访问:虚拟机ip:80
成功访问
⽬录核⼼介绍
- conf #所有配置文件目录
- nginx.conf #默认的主要的配置⽂件
- nginx.conf.default #默认模板
-
- html # 这是编译安装时Nginx的默认站点目录
- 50x.html #错误页面
- index.html #访问index默认首页面
-
- logs # nginx默认的日志路径,包括错误⽇志及访问日志
- error.log #错误日志
- nginx.pid #nginx启动后的进程id
- access.log #nginx访问日志
-
- sbin #nginx命令的⽬录
- nginx #启动命令
-
- # 每个配置项由配置指令和指令参数 2 个部分构成
- #user nobody; # 指定Nginx Worker进程运行以及用户组
- worker_processes 1;
-
- #error_log logs/error.log; # 错误日志的存放路径和错误日志
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
-
- #pid logs/nginx.pid; # 进程PID存放路径
-
-
- #事件模块指令,用来指定Nginx的IO模型,Nginx支持的有select、poll、kqueue、epoll 等。
- #不同的是epoll用在Linux平台上,而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选
- events {
-
- use epoll;
-
- #定义Nginx每个进程的最大连接数,作为服务器来说:worker_connections*worker_processes
- #作为反向代理来说,最大并发数量应该是worker_connections*worker processes/2。因为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接
- worker_connections 1024;
- }
-
-
- http {
- include mime.types;
- default_type application/octet-stream;
-
- #自定义服务日志 其中main指模板名称,后面的内容是日志的输出格式
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
-
- #nginx的访问日志文件位置 及 使用哪一个模板
- #以下配置表示 用户访问了nginx时客户端信息都会按照main模板定义的格式记录在access.log日志文件当中
- #access_log logs/access.log main;
-
- # 是否开启高效传输模式 on开启 off关闭
- sendfile on;
-
- #减少网络报文段的数量
- #tcp_nopush on;
-
- #keepalive_timeout 0;
-
- #客户端连接保持活动的超时时间,超过这个时间之后,服务器会关闭该连接
- keepalive_timeout 65;
-
- #gzip on;
-
- #虚拟主机的配置(什么是虚拟主机:指在⼀台物理主机服务器上划分出多个磁盘空间,
- 每个磁盘空间都是⼀个虚拟主机,每台虚拟主机都可以对外提供Web服务,并且互不⼲扰,就类似虚拟机,
- 利⽤虚拟主机把多个不同域名的⽹站部署在同⼀台服务器上,节省了服务器硬件成本和相关的维护费⽤)
- server {
- listen 80; #虚拟主机的服务端口
- server_name localhost; #用来指定IP地址或域名,多个域名之间用空格分开
-
- #charset koi8-r;
-
- #access_log logs/host.access.log main;
-
- #URL地址匹配
- location / {
- root html; # 服务默认启动目录.root表示一个路径,可以是相对路径,也可以说是绝对路径
- index index.html index.htm; # html目录下默认访问文件index.html,按照顺序找
- }
-
- #error_page 404 /404.html; #错误状态码的显示页面
-
- # redirect server error pages to the static page /50x.html
- #
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
-
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #}
-
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
-
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
-
-
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
-
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
-
-
- # HTTPS server
- #
- #server {
- # listen 443 ssl;
- # server_name localhost;
-
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
-
- # ssl_session_cache shared:SSL:1m;
- # ssl_session_timeout 5m;
-
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
-
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
-
- }
-
使用nginx的命令必须进入 /usr/local/nginx/sbin 目录,才能执行nginx的相关命令
查看nginx版本
- ./nginx -v
-
关闭nginx服务器
- ./nginx -s stop
-
启动nginx
- ./nginx
-
重新加载命令(一般是修改了nginx的配置文件之后执行)
- ./nginx -s reload
-
在上述我们在使用nginx命令在进行服务的启动、停止、重新加载时,都需要用到一个指令nginx,而这个指令是在nginx/sbin目录下的,我们每一次使用这个指令都需要切换到sbin目录才可以,使用相对繁琐。那么我们能不能在任意目录下都可以执行该指令来操作nginx呢?答案是可以的,配置nginx的环境变量即可。
通过vim编辑器,打开/etc/profile文件, 在PATH环境变量中增加nginx的sbin目录,如下:
- MAVEN_HOME=/usr/local/software/apache-maven-3.5.4
- PATH=/usr/local/nginx/sbin:$PATH:$MAVEN_HOME/bin
- export PATH JAVA_HOME CLASSPATH MAVEN_HOME
-
之后在任意路径下执行
查看nginx版本
- nginx -v
-
关闭nginx服务器
- nginx -s stop
-
启动nginx
- nginx
-
重新加载命令(一般是修改了nginx的配置文件之后执行)
- nginx -s reload
-
本地域名映射
浏览器输入要访问某个网站地址的时候(域名访问),会有解析域名的情况,首先如果在电脑本地的host文件当中配置域名解析为ip的话,那就直接使用本地的解析的ip,如果本地没有对应的解析,就去使用互联网上的NDS
就好比我们在浏览器输入localhost这个域名的时候,它就会被解析127.0.0.1
为什么呢?因为我们在本地配置了域名映射
- 浏览器输⼊个域名,经过DNS解析获取IP,如果我们没有公⽹域名和IP,怎么去测试这块内容?
- DNS解析,会先获取本地的Host⽂件,先看本地有没域名>IP 的映射,有的话直接使⽤
-
接下来就对上面访问虚拟机上的nginx做本地域名映射虚拟机IP
首先找到host文件:C:\Windows\System32\drivers\etc
测试通过域名访问虚拟机上的nginx