02-Nginx的基本配置与优化

    科技2022-07-11  90

    1.1 Nginx的完整配置示例

    #使用的用户和组 #user nobody; #指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,例如两个四核CPU,则总核数为8) worker_processes 1; #指定错误日志存放的路径,错误日志记录级别可选项为(debug|info|notice|warn|error|crit) #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #指定pid存放的路径 #pid logs/nginx.pid; events { #允许的连接数 worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #设置字符集 #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index aa.html index.html index.htm; } #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; # } #} }

    1.2 Nginx的虚拟主机配置

    1> 什么是虚拟主机

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

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

    基于IP的虚拟主机

    基于域名的虚拟主机

    基于端口的虚拟主机

    2>基于IP的虚拟主机

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

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

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

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

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

    添加前配置

    NAME=ens33 DEVICE=ens33 TYPE=Ethernet HWADDR=00:50:56:20:8F:89 NM_CONTROLLED=yes ONBOOT=yes IPADDR=192.168.254.210 BOOTPROTO=static NETMASK=255.255.255.0 GATEWAY=192.168.254.2 DNS1=192.168.254.2 IPV6INIT=no ZONE=public

    添加后

    TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=ens33 UUID=933cdc9b-b383-4ddd-b219-5a72c69c9cf0 ONBOOT=yes HWADDR=00:50:56:20:8F:89 IPADDR0=192.168.254.210 IPADDR1=192.168.254.211 IPADDR2=192.168.254.212 PREFIX0=24 GATEWAY0=192.168.254.2 DNS1=192.168.254.2 IPV6_PEERDNS=yes IPV6_PEERROUTES=yes

    查看ip地址

    [root@base210 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:50:56:20:8f:89 brd ff:ff:ff:ff:ff:ff inet 192.168.254.210/24 brd 192.168.254.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.254.211/24 brd 192.168.254.255 scope global secondary noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.254.212/24 brd 192.168.254.255 scope global secondary noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe20:8f89/64 scope link noprefixroute 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  

    #第一个虚拟机 server { #监听的IP和端口 listen 192.168.254.210:80; #主机名称 server_name 192.168.254.210; #charset koi8-r; #访问日志文件存放路径 access_log logs/server1.access.log combined; location / { #html网页文件存放的目录 root server1; #默认首页文件,顺序从左到右 index index.html index.htm; } #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; #} } #第二个虚拟主机 server{ listen 192.168.254.211:80; server_name 192.168.254.211; access_log logs/server2.access.log combined; location / { root server2; index index.html index.htm; } } #第三个虚拟主机 server{ listen 192.168.254.212:80; server_name 192.168.254.212; access_log logs/server3.access.log combined; location / { root server3; index index.html index.htm; } }

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

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

    <html> this is server1 page </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 配置

          

    server { listen 80; #域名配置 server_name a.domain.com; #charset koi8-r; access_log logs/a.domain.com.access.log combined; location / { root server1; index index.html index.htm; } #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; #} } server{ listen 80; #域名配置 server_name b.domain.com; access_log logs/b.domain.com.access.log combined; location / { root server2; index index.html index.htm; } } server{ listen 80; #域名配置 server_name b.domain.com; access_log logs/b.domain.com.access.log combined; location / { root server3; index index.html index.htm; } }

    从容重启nginx 使其加载配置

    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日志相关的指令主要有两个

    log_format 用来设置日志的格式另外一条是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;

     

     

     

     

     

    Processed: 0.023, SQL: 8