[关闭]
@xushengkai 2022-12-08T05:13:20.000000Z 字数 4393 阅读 366

Nginx基础篇

nginx


[TOC]

nginx的编译安装部署

  1. #安装源代码编译需要用的工具
  2. [root@192 ~]# yum -y install pcre-devel openssl-devel make gcc gcc-c++ cmake gmake
  3. #官网下载源码包
  4. [root@192 ~]# wget -q http://nginx.org/download/nginx-1.10.2.tar.gz
  5. #创建程序用户
  6. [root@192 ~]# useradd -s /sbin/nologin -M nginx
  7. #解压缩
  8. [root@192 ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
  9. #进行编译安装
  10. [root@192 ~]# cd /usr/src/nginx-1.10.2
  11. [root@192 nginx-1.10.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  12. [root@192 nginx-1.10.2]# make && make install
  13. #给nginx命令做软链接
  14. [root@192 nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
  15. #启动nginx
  16. [root@192 nginx-1.10.2]# /usr/local/nginx/sbin/nginx
  17. [root@192 nginx-1.10.2]# ss -anp | grep nginx
  18. LISTEN 0 128 *:80 *:* users:(("nginx",3754,6),("nginx",3755,6))

/usr/local/nginx/sbin/nginx -s reload nginx平滑重启命令
/usr/local/nginx/sbin/nginx -s stop nginx停止服务命令

Nginx主配置文件nginx.conf

源代码编译安装的nginx配置文件在/usr/local/nginx/conf/下

  1. worker_processes 1; #worker进程的数量,可以设置为auto
  2. #error_log logs/error.log; #错误日志(默认关闭)
  3. #pid logs/nginx.pid; #进程号(默认关闭)
  4. events { #事件区块开始
  5. worker_connections 1024; #每个worker进程支持的最大连接数,一般设置20480;
  6. } #事件区块结束
  7. http { #http区块开始
  8. include mime.types; #Nginx支持的媒体类型库文件包含
  9. default_type application/octet-stream; #默认的媒体类型
  10. sendfile on; #开启高效传输模式
  11. keepalive_timeout 65; #连接保持超时时间
  12. server { #网站配置区域(第一个server第一个虚拟主机站点)
  13. listen 80; #提供服务的端口,默认80
  14. server_name localhost; #提供服务的域名主机名
  15. location / { #第一个Location区块开始
  16. root html; #站点的根目录(相对于nginx安装路径)
  17. index index.html index.htm; #默认的首页文件,多个用空格分开
  18. }
  19. error_page 500 502 503 504 /50x.html; #出现对应的http状态码时,使用50x.html回应客户
  20. location = /50x.html { #Location区块开始,访问50x.html
  21. root html; #指定对应的站点目录为html
  22. }
  23. }
  24. server { #网站配置区域(第二个server第二个虚拟主机站点)
  25. listen 80; #提供服务的端口,默认80
  26. server_name bbs.chensiqi.org; #提供服务的域名主机名
  27. location / { #服务区块
  28. root html; #相对路径(nginx安装路径)
  29. index index.html index.htm;
  30. }
  31. location = /50x.html { #发生错误访问的页面
  32. root html;
  33. }
  34. }
  35. }
  36. 整个nginx配置文件的核心框架如下:
  37. worker_processes 1;
  38. events {
  39. worker_connections 1024;
  40. }
  41. http {
  42. include mime.types;
  43. server {
  44. listen 80;
  45. server_name localhost;
  46. location / {
  47. root html;
  48. index index.html index.htm;
  49. }
  50. }
  51. }

虚拟主机配置

所谓虚拟主机,在Web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。
这个独立的站点在配置里是由一定格式的标签段标记,对于Apache软件来说,一个虚拟主机的标签段通常被包含在内,而Nginx软件则使用一个server{}标签来标示一个虚拟主机,一个Web服务里可以有多个虚拟主机标签对,即同时可以支持多个虚拟主机站点。


基于域名的虚拟主机

所谓基于域名的虚拟主机,意思就是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站都是使用基于域名的虚拟主机,


示例

  1. [root@192 ~]# cd /usr/local/nginx/conf/
  2. [root@192 conf]# mkdir conf.d
  3. [root@192 conf]# cd conf.d/
  4. [root@192 conf.d]# touch a.com.conf b.com.conf
  5. #设置a.com的页面
  6. [root@192 conf.d]# vim a.com.conf
  7. server {
  8. listen 80;
  9. server_name a.com;
  10. location / {
  11. root html/a.com;
  12. index index.html index.htm;
  13. }
  14. }
  15. #设置b.com的页面
  16. [root@192 conf.d]# vim b.com.conf
  17. server {
  18. listen 80;
  19. server_name b.com;
  20. location / {
  21. root html/b.com;
  22. index index.html index.htm;
  23. }
  24. }
  25. #在主配置文件中将keepalive_timeout 65;以下的内容删掉
  26. #先备份在修改
  27. [root@192 nginx]# cd /usr/local/nginx/
  28. [root@192 nginx]# cp conf/nginx.conf conf/nginx.conf.bak
  29. [root@192 nginx]# vim conf/nginx.conf
  30. 23 keepalive_timeout 65;
  31. 24 #添加以下两行内容
  32. 25 include conf.d/a.com.conf;
  33. 26 include conf.d/b.com.conf;
  34. 27
  35. 28 }
  36. #给创建a.com和b.com的默认页面
  37. [root@192 nginx]# cd html/
  38. [root@192 html]# mkdir a.com b.com
  39. [root@192 html]# echo "111 a.com " >> a.com/index.html
  40. [root@192 html]# echo "222 b.com " >> b.com/index.html
  41. #重启nginx
  42. [root@192 nginx]# /usr/local/nginx/sbin/nginx -s reload

在主机的hosts文件设置域名解析

  1. 192.168.200.139 a.com b.com

测试

在主机上访问a.com,显示内容:111 a.com
在主机上访问b.com,显示内容:222 b.com
实验成功

基于端口的虚拟主机

基于端口的虚拟主机,意思就是通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不希望直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机地址里要带有端口


示例

  1. #在主配置文件中设置不同端口的页面位置
  2. [root@192 html]# cd ../conf/
  3. #先备份,在修改
  4. [root@192 conf]# cp nginx.conf nginx.conf.bak
  5. [root@192 conf]# vim nginx.conf
  6. 1 worker_processes 1;
  7. 2
  8. 3 events {
  9. 4 worker_connections 1024;
  10. 5 }
  11. 6
  12. 7 http {
  13. 8 include mime.types;
  14. 9 default_type application/octet-stream;
  15. 10
  16. 11 sendfile on;
  17. 12 keepalive_timeout 65;
  18. 13 #将此行的下面内容全部删掉并添加以下内容
  19. ##www的默认页面及端口
  20. 14 server {
  21. 15 listen 80; #端口80
  22. 16 server_name localhost;
  23. 17
  24. 18 location / {
  25. 19 root html/www; #默认页面的位置
  26. 20 index index.html index.htm;
  27. 21 }
  28. 22 }
  29. 23 #bbs的默认页面位置及端口
  30. 24 server {
  31. 25 listen 81; #端口
  32. 26 server_name localhost;
  33. 27
  34. 28 location / {
  35. 29 root html/bbs; #默认页面的位置
  36. 30 index index.html index.htm;
  37. 31 }
  38. 32 }
  39. 33 }
  40. #创建www,bbs的页面
  41. [root@192 nginx]# cd /usr/local/nginx/html
  42. [root@192 html]# mkdir www bbs
  43. [root@192 html]# echo "welcome to wwww" >> www/index.html
  44. [root@192 html]# echo "welcome to bbs" >> bbs/index.html
  45. #重启nginx
  46. [root@192 conf]# /usr/local/nginx/sbin/nginx -s reload

测试

在主机上打开浏览器
测试www页面,访问http://192.168.200.139:80,显示内容:welcome to wwww
测试bbs页面,访问http://192.168.200.139:80,显示内容:welcome to bbs


测试成功

基于不同IP的虚拟主机

基于IP的虚拟主机,是通过不同的IP区分不同的虚拟主机,此类虚拟主机对应的企业应用非常少见,一般不同业务需要使用多IP的场景都会在负载均衡器上进行IP绑定,而不是在Web上通过绑定IP区分不同的虚拟机。
了解即可

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注