02-Nginx的基本配置与优化

深藏阁楼爱情的钟 2023-10-11 14:39 100阅读 0赞

1.1 Nginx的完整配置示例

  1. #使用的用户和组
  2. #user nobody;
  3. #指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,例如两个四核CPU,则总核数为8)
  4. worker_processes 1;
  5. #指定错误日志存放的路径,错误日志记录级别可选项为(debug|info|notice|warn|error|crit)
  6. #error_log logs/error.log;
  7. #error_log logs/error.log notice;
  8. #error_log logs/error.log info;
  9. #指定pid存放的路径
  10. #pid logs/nginx.pid;
  11. events {
  12. #允许的连接数
  13. worker_connections 1024;
  14. }
  15. http {
  16. include mime.types;
  17. default_type application/octet-stream;
  18. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  19. # '$status $body_bytes_sent "$http_referer" '
  20. # '"$http_user_agent" "$http_x_forwarded_for"';
  21. #access_log logs/access.log main;
  22. sendfile on;
  23. #tcp_nopush on;
  24. #keepalive_timeout 0;
  25. keepalive_timeout 65;
  26. #gzip on;
  27. server {
  28. listen 80;
  29. server_name localhost;
  30. #设置字符集
  31. #charset koi8-r;
  32. #access_log logs/host.access.log main;
  33. location / {
  34. root html;
  35. index aa.html index.html index.htm;
  36. }
  37. #error_page 404 /404.html;
  38. # redirect server error pages to the static page /50x.html
  39. #
  40. error_page 500 502 503 504 /50x.html;
  41. location = /50x.html {
  42. root html;
  43. }
  44. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  45. #
  46. #location ~ \.php$ {
  47. # proxy_pass http://127.0.0.1;
  48. #}
  49. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  50. #
  51. #location ~ \.php$ {
  52. # root html;
  53. # fastcgi_pass 127.0.0.1:9000;
  54. # fastcgi_index index.php;
  55. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  56. # include fastcgi_params;
  57. #}
  58. # deny access to .htaccess files, if Apache's document root
  59. # concurs with nginx's one
  60. #
  61. #location ~ /\.ht {
  62. # deny all;
  63. #}
  64. }
  65. # another virtual host using mix of IP-, name-, and port-based configuration
  66. #
  67. #server {
  68. # listen 8000;
  69. # listen somename:8080;
  70. # server_name somename alias another.alias;
  71. # location / {
  72. # root html;
  73. # index index.html index.htm;
  74. # }
  75. #}
  76. # HTTPS server
  77. #
  78. #server {
  79. # listen 443 ssl;
  80. # server_name localhost;
  81. # ssl_certificate cert.pem;
  82. # ssl_certificate_key cert.key;
  83. # ssl_session_cache shared:SSL:1m;
  84. # ssl_session_timeout 5m;
  85. # ssl_ciphers HIGH:!aNULL:!MD5;
  86. # ssl_prefer_server_ciphers on;
  87. # location / {
  88. # root html;
  89. # index index.html index.htm;
  90. # }
  91. #}
  92. }

1.2 Nginx的虚拟主机配置

1> 什么是虚拟主机

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机,分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Internet服务器功能,同一台主机上的虚拟主机之间是完全独立的。

Nginx可以配置多种类型的虚拟主机

  • 基于IP的虚拟主机
  • 基于域名的虚拟主机
  • 基于端口的虚拟主机

2>基于IP的虚拟主机

用ifconfig命令查看该服务器的IP地址,这台服务器有一块物理网卡设备ens33 ip地址为192.168.254.210 和 本地回环设备lo ip地址为:127.0.0.1

  1. ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  2. inet 192.168.254.210 netmask 255.255.255.0 broadcast 192.168.254.255
  3. inet6 fe80::250:56ff:fe20:8f89 prefixlen 64 scopeid 0x20<link>
  4. ether 00:50:56:20:8f:89 txqueuelen 1000 (Ethernet)
  5. RX packets 8424 bytes 6893884 (6.5 MiB)
  6. RX errors 0 dropped 0 overruns 0 frame 0
  7. TX packets 4932 bytes 581425 (567.7 KiB)
  8. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  9. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  10. inet 127.0.0.1 netmask 255.0.0.0
  11. inet6 ::1 prefixlen 128 scopeid 0x10<host>
  12. loop txqueuelen 1000 (Local Loopback)
  13. RX packets 32 bytes 2592 (2.5 KiB)
  14. RX errors 0 dropped 0 overruns 0 frame 0
  15. TX packets 32 bytes 2592 (2.5 KiB)
  16. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

本地回环代表设备的本地虚拟接口,所以默认被看作是永远不会宕掉的接口,它的作用有两个:

  1. 测试本机的网络配置,能ping通127.0.0.1 说明本机的网卡 和IP协议安装都没有问题。
  2. 某些server/client的应用程序在运行时需调用服务器上的资源,一般要指定Server 的ip地址,当该程序要在同一台机器上运行且没有别的Server时,就可以把Server的资源装在本机上,Server 的IP地址设为 127.0.0.1 也同样可以运行。

为ens33 网卡设备添加192.168.254.211,192.168.254.212 ip地址

添加前配置

  1. NAME=ens33
  2. DEVICE=ens33
  3. TYPE=Ethernet
  4. HWADDR=00:50:56:20:8F:89
  5. NM_CONTROLLED=yes
  6. ONBOOT=yes
  7. IPADDR=192.168.254.210
  8. BOOTPROTO=static
  9. NETMASK=255.255.255.0
  10. GATEWAY=192.168.254.2
  11. DNS1=192.168.254.2
  12. IPV6INIT=no
  13. ZONE=public

添加后

  1. TYPE=Ethernet
  2. BOOTPROTO=none
  3. DEFROUTE=yes
  4. IPV4_FAILURE_FATAL=no
  5. IPV6INIT=yes
  6. IPV6_AUTOCONF=yes
  7. IPV6_DEFROUTE=yes
  8. IPV6_FAILURE_FATAL=no
  9. NAME=ens33
  10. UUID=933cdc9b-b383-4ddd-b219-5a72c69c9cf0
  11. ONBOOT=yes
  12. HWADDR=00:50:56:20:8F:89
  13. IPADDR0=192.168.254.210
  14. IPADDR1=192.168.254.211
  15. IPADDR2=192.168.254.212
  16. PREFIX0=24
  17. GATEWAY0=192.168.254.2
  18. DNS1=192.168.254.2
  19. IPV6_PEERDNS=yes
  20. IPV6_PEERROUTES=yes

查看ip地址

  1. [root@base210 ~]# ip addr
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  9. link/ether 00:50:56:20:8f:89 brd ff:ff:ff:ff:ff:ff
  10. inet 192.168.254.210/24 brd 192.168.254.255 scope global noprefixroute ens33
  11. valid_lft forever preferred_lft forever
  12. inet 192.168.254.211/24 brd 192.168.254.255 scope global secondary noprefixroute ens33
  13. valid_lft forever preferred_lft forever
  14. inet 192.168.254.212/24 brd 192.168.254.255 scope global secondary noprefixroute ens33
  15. valid_lft forever preferred_lft forever
  16. inet6 fe80::250:56ff:fe20:8f89/64 scope link noprefixroute
  17. valid_lft forever preferred_lft forever

从window ping 192.168.254.211,192.168.254.212 都能ping通

为Nginx配置文件(nginx.conf),分别对192.168.254.211,192.168.254.212,192.168.254.210 三个IP配置三个纯静态HTML支持的虚拟主机

编辑 conf/nginx.conf

  1. #第一个虚拟机
  2. server {
  3. #监听的IP和端口
  4. listen 192.168.254.210:80;
  5. #主机名称
  6. server_name 192.168.254.210;
  7. #charset koi8-r;
  8. #访问日志文件存放路径
  9. access_log logs/server1.access.log combined;
  10. location / {
  11. #html网页文件存放的目录
  12. root server1;
  13. #默认首页文件,顺序从左到右
  14. index index.html index.htm;
  15. }
  16. #error_page 404 /404.html;
  17. # redirect server error pages to the static page /50x.html
  18. #
  19. error_page 500 502 503 504 /50x.html;
  20. location = /50x.html {
  21. root html;
  22. }
  23. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  24. #
  25. #location ~ \.php$ {
  26. # proxy_pass http://127.0.0.1;
  27. #}
  28. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  29. #
  30. #location ~ \.php$ {
  31. # root html;
  32. # fastcgi_pass 127.0.0.1:9000;
  33. # fastcgi_index index.php;
  34. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  35. # include fastcgi_params;
  36. #}
  37. # deny access to .htaccess files, if Apache's document root
  38. # concurs with nginx's one
  39. #
  40. #location ~ /\.ht {
  41. # deny all;
  42. #}
  43. }
  44. #第二个虚拟主机
  45. server{
  46. listen 192.168.254.211:80;
  47. server_name 192.168.254.211;
  48. access_log logs/server2.access.log combined;
  49. location / {
  50. root server2;
  51. index index.html index.htm;
  52. }
  53. }
  54. #第三个虚拟主机
  55. server{
  56. listen 192.168.254.212:80;
  57. server_name 192.168.254.212;
  58. access_log logs/server3.access.log combined;
  59. location / {
  60. root server3;
  61. index index.html index.htm;
  62. }
  63. }

在 /usr/local/nginx 下创建 server1、server2、server3目录,每个目录中添加index.html文件

内容分别为 this is server1 page、this is server2 page、this is server3 page

  1. <html>
  2. this is server1 page
  3. </html>

设置好后,重启加载新配置文件

在浏览器中通过192.168.254.210:80 可以访问到 this is server1 page

在浏览器中通过192.168.254.211:80 可以访问到 this is server2 page

在浏览器中通过192.168.254.212:80 可以访问到 this is server3 page

配置基于域名的虚拟主机

基于域名的虚拟主机是最常见的一种虚拟主机,只需配置你的DNS服务器,将每个主机名映射到正确的IP地址,然后配置Nginx服务器,令其识别不同的主机名就可以了。

可以使多个虚拟主机共享同一个IP地址,有效解决了IP地址不足的问题。

更改nginx.conf 配置

  1. server {
  2. listen 80;
  3. #域名配置
  4. server_name a.domain.com;
  5. #charset koi8-r;
  6. access_log logs/a.domain.com.access.log combined;
  7. location / {
  8. root server1;
  9. index index.html index.htm;
  10. }
  11. #error_page 404 /404.html;
  12. # redirect server error pages to the static page /50x.html
  13. #
  14. error_page 500 502 503 504 /50x.html;
  15. location = /50x.html {
  16. root html;
  17. }
  18. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  19. #
  20. #location ~ \.php$ {
  21. # proxy_pass http://127.0.0.1;
  22. #}
  23. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  24. #
  25. #location ~ \.php$ {
  26. # root html;
  27. # fastcgi_pass 127.0.0.1:9000;
  28. # fastcgi_index index.php;
  29. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  30. # include fastcgi_params;
  31. #}
  32. # deny access to .htaccess files, if Apache's document root
  33. # concurs with nginx's one
  34. #
  35. #location ~ /\.ht {
  36. # deny all;
  37. #}
  38. }
  39. server{
  40. listen 80;
  41. #域名配置
  42. server_name b.domain.com;
  43. access_log logs/b.domain.com.access.log combined;
  44. location / {
  45. root server2;
  46. index index.html index.htm;
  47. }
  48. }
  49. server{
  50. listen 80;
  51. #域名配置
  52. server_name b.domain.com;
  53. access_log logs/b.domain.com.access.log combined;
  54. location / {
  55. root server3;
  56. index index.html index.htm;
  57. }
  58. }

从容重启nginx 使其加载配置

  1. kill -HUP 1426

在window电脑上更改hosts 测试

192.168.254.210 a.domain.com
192.168.254.211 b.domain.com
192.168.254.212 c.domain.com

通过浏览器访问 a.domain.com 可以访问到this is server1 page 内容

Nginx的日志文件配置与切割

与Nginx日志相关的指令主要有两个

  1. log_format 用来设置日志的格式
  2. 另外一条是access_log 用来指定日志文件的存放路径、格式、缓存大小。

1> 用log_format 指令设置日志的格式,语法

log_format name format [format]

其中name表示定义为格式名称,format表示定义为格式样式。

log_format有一个默认的,无需设置的combined日志格式设置,相当于Apache的combined 日志格式,其具体参数如下:

log_format combined ‘$remote_addr-$remote_user [$time_local]‘

‘“$request” $status $body_bytes_sent’

‘“$http_refer” “$http_user_agent”‘;

也可以自定义一份日志的记录格式,不过要注意,log_format指令设置的name名称在Nginx配置文件中是不能重复的。

2>用access_log指令指定日志文件的存放路径

— 如果不想记录日志,可以使用以下指令关闭日志记录:access_log off;

—如果想使用默认的combined 格式的日志记录,可以使用以下实例

access_log /data1/logs/filename.log combined;

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nginx基本配置、性能优化指南

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了!而且,在大多数情况下,一个常规安装的ngi