• Nginx网站服务


     

    目录

     一、Nginx概述

    二、Nginx配置文件

    1、全局配置

     2、I/O事件

     3、HTTO配置

    三、访问控制

     四、nginx虚拟主机应用

    1、基于域名的 Nginx 虚拟主机

     2、基于端口的虚拟主机

     3、基于不同IP访问


    一、Nginx概述

    Nginx是一款高性能、轻量级的wdb服务软件

    • 稳定性高
    • 系统资源消耗低 (协程、回调)
    • 对HTTP并发连接的处理能力高 (epoll I/O多路复用)
      • 理论上单台物理服务器可支持30000~50000个并发请求(实际大概20000~30000)

    二、Nginx配置文件

    1、全局配置

    1. #user nobody; #默认运行/管理用户
    2. worker_processes 1; #工作进程运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1
    3. #error_log logs/error.log; #错误日志文件路径/级别
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid; #pid文件位置

     2、I/O事件

    1. events { #events:事件
    2. use epoll; #epoll是一种抗高并发的参数之一
    3. worker_connections 1024; #每个进程最多处理的连接数量
    4. }
    5. 进程处理的连接数量有两种修改方式,以下是临时修改
    6. #如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
    7. #在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
    8. #可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制.

     3、HTTO配置

    1. http { #http协议的配置
    2. include mime.types; #文件扩展名与文件类型映射表 用include识别文件
    3. default_type application/octet-stream; #默认文件类型
    4. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    5. #日志格式设置
    6. # '$status $body_bytes_sent "$http_referer" '
    7. # '"$http_user_agent" "$http_x_forwarded_for"';
    8. #access_log logs/access.log main; #访问日志位置
    9. sendfile on; ##支持文件发送(下载)
    10. #tcp_nopush on; #此项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
    11. #keepalive_timeout 0; ##连接保持超时时间,单位:秒
    12. keepalive_timeout 65;
    13. #gzip on; #压缩模块 on 表示开启
    14. server { #web 服务相关的一些配置
    15. listen 80; #默认监听端口
    16. server_name localhost; #站点域名
    17. #charset koi8-r; #字符集支持(修改为中文)UTF-8
    18. #access_log logs/host.access.log main; #此web服务的主访问日志
    19. location / { #“/”根目录配置 (浏览器中,www.baidu.com/
    20. root html; #网站根目录的位置/usr/local/nginx/html(相对路径)
    21. index index.html index.htm; #支持的首页文件格式
    22. }
    23. #error_page 404 /404.html;
    24. # redirect server error pages to the static page /50x.html
    25. #
    26. error_page 500 502 503 504 /50x.html; #当发生错误的时候能够显示一个预定义的错误页面
    27. location = /50x.html { #错误页面配置
    28. root html;
    29. }

    支持PHP及跳转的配置

    1. # proxy(代理) the PHP scripts to Apache listening on 127.0.0.1:80
    2. #
    3. #location ~ \.php$ {
    4. # proxy_pass http://127.0.0.1;
    5. #}
    6. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    7. #
    8. #location ~ \.php$ {
    9. # root html;
    10. # fastcgi_pass 127.0.0.1:9000;
    11. # fastcgi_index index.php;
    12. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    13. # include fastcgi_params;
    14. #}
    15. # deny access to .htaccess files, if Apache's document root
    16. # concurs with nginx's one
    17. #
    18. #location ~ /\.ht {
    19. # deny all;
    20. #}
    21. }

    虚拟主机的配置

    1. # another virtual host using mix of IP-, name-, and port-based configuration
    2. #
    3. #server {
    4. # listen 8000;
    5. # listen somename:8080;
    6. # server_name somename alias another.alias;
    7. # location / {
    8. # root html;
    9. # index index.html index.htm;
    10. # }
    11. #}

    HTTPS的配置(SSL 加密) 

    1. # HTTPS server
    2. #
    3. #server {
    4. # listen 443 ssl;
    5. # server_name localhost;
    6. # ssl_certificate cert.pem;
    7. # ssl_certificate_key cert.key;
    8. # ssl_session_cache shared:SSL:1m;
    9. # ssl_session_timeout 5m;
    10. # ssl_ciphers HIGH:!aNULL:!MD5;
    11. # ssl_prefer_server_ciphers on;
    12. # location / {
    13. # root html;
    14. # index index.html index.htm;
    15. # }
    16. #}

    三、访问控制

    1. deny IP/IP段:拒绝某个 IP 或 IP段的客户端访问
    2. allow IP/IP段:允许某个 IP 或IP 段的客户端的访问
    3. 规则从上往下执行,如匹配则停止,不再往下匹配

     

     

     四、nginx虚拟主机应用

    nginx支持的虚拟主机

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

    通过 "server {}" 配置段实现

    1、基于域名的 Nginx 虚拟主机

    添加域名解析

    echo "192.168.226.128 www.accp.com www.benet.com" >> /etc/hosts

     准备虚拟站点网页文档

    1. mkdir -p /var/www/html/accp
    2. mkdir -p /var/www/html/benet
    3. echo "

      www.accp.com

      "
      >/var/www/html/accp/index.html
    4. echo "

      www.benet.com

      "
      >/var/www/html/benet/index.html

     修改配置文件

    1. http {
    2. ……
    3. server {
    4. listen 80;
    5. server_name www.accp.com;
    6. charset utf-8;
    7. access_log logs/accp.access.log;
    8. location / {
    9. root /var/www/html/accp;
    10. index index.html index.htm;
    11. }
    12. error_page 500 502 503 504 /50x.html;
    13. location = /50x.html {
    14. root html;
    15. }
    16. }
    17. server {
    18. listen 80;
    19. server_name www.benet.com;
    20. charset utf-8;
    21. access_log logs/benet.access.log;
    22. location / {
    23. root /var/www/html/benet;
    24. index index.html index.htm;
    25. }
    26. error_page 500 502 503 504 /50x.html;
    27. location = /50x.html {
    28. root html;
    29. }
    30. }

     

     

     重启nginx,测试

    systemctl restart nginx

     2、基于端口的虚拟主机

     创建8080端口的网页文件

    echo "

    www.accp8080.com

    "
    >/var/www/html/accp8080/index.html

     进入配置文件修改

    1. 进入配置文件 复制www.accp.com的配置,修改端口号
    2. server { #原accp配置
    3. listen 192.168.226.128:80; #指向监听端口
    4. server_name www.accp.com;
    5. charset utf-8;
    6. access_log logs/accp.access.log;
    7. location / {
    8. root /var/www/html/accp;
    9. index index.html index.htm;
    10. }
    11. error_page 500 502 503 504 /50x.html;
    12. location = /50x.html {
    13. root html;
    14. }
    15. }
    16. server { #新accp配置
    17. listen 192.168.226.128:8080; #指向8080端口
    18. server_name www.accp.com;
    19. charset utf-8;
    20. access_log logs/accp8080.access.log; #便于区分,指定生成不同日志
    21. location / {
    22. root /var/www/html/accp8080; #指向8080端口的站点首页
    23. index index.html index.htm;
    24. }
    25. error_page 500 502 503 504 /50x.html;
    26. location = /50x.html {
    27. root html;
    28. }
    29. }

     重启nginx,测试

     3、基于不同IP访问

    添加映射

    1. 添加192.168.226.100的映射
    2. vim /etc/hosts
    3. 192.168.226.100 www.benet.com

    创建网站根目录、创建网站首页文件(index.html)

    1. mkdir /var/www/html/benet100
    2. echo "

      www.benet100.com

      "
      >> /var/www/html/benet100/index.html

     

     临时创建虚拟网卡

    ifconfig ens33:0 192.168.226.100 netmask 255.255.255.0

     修改配置文件

    1. server {
    2. listen 192.168.226.128:80;
    3. server_name www.accp.com;
    4. charset utf-8;
    5. access_log logs/accp.access.log;
    6. location / {
    7. root /var/www/html/accp;
    8. index index.html index.htm;
    9. }
    10. error_page 500 502 503 504 /50x.html;
    11. location = /50x.html {
    12. root html;
    13. }
    14. }
    15. server {
    16. listen 192.168.226.100:80; #benet监听的IP修改为100
    17. server_name www.benet.com;
    18. charset utf-8;
    19. access_log logs/benet100.access.log;
    20. location / {
    21. root /var/www/html/benet100;
    22. index index.html index.htm;
    23. }
    24. error_page 500 502 503 504 /50x.html;
    25. location = /50x.html {
    26. root html;
    27. }
    28. }

     

    重启nginx,测试

     

  • 相关阅读:
    常见的内网穿透代理工具
    C++ Reference: Standard C++ Library reference: C Library: cwchar: wcscoll
    Java变量的声明和初始化
    如何与墨西哥大众VW Mexico建立EDI连接
    带你吃透Servlet技术(一)
    根据模板动态生成word(一)使用freemarker生成word
    Camunda 7.x 系列【50】任务服务 TaskService
    pandas中read_csv和to_csv、read_hdf和to_hdf、read_json和to_json函数及其他各类文件的读取与存储
    【相位解缠】一维信号相位解包裹原理及其演示实例
    C语言 循环结构
  • 原文地址:https://blog.csdn.net/qq_69278945/article/details/126569505