Nginx的介绍以及配置使用

介绍

Nginx 作为一种高性能的开源 Web 服务器软件,以其卓越的性能、灵活的配置和强大的功能,成为众多网站搭建的首选。本博客将深入探讨 Nginx 的核心概念、配置方法以及实际应用场景,帮助读者全面了解 Nginx 在构建高效网站中的重要作用。

作用

反向代理:Nginx 可以作为反向代理服务器,将客户端请求转发到后端的服务器上。当用户向网站发送请求时,Nginx 接收请求并根据配置规则将请求转发到相应的后端服务器。这不仅提高了网站的安全性,还可以实现负载均衡,提升网站的性能和可用性。

负载均衡:通过配置 Nginx,可以将请求均匀地分配到多个后端服务器上,避免单个服务器负载过高。Nginx 支持多种负载均衡算法,如轮询、加权轮询、IP 哈希等,根据实际需求选择合适的算法,确保服务器资源得到充分利用。

静态资源缓存:Nginx 能够缓存静态资源,如图片、CSS、JavaScript 等。当用户请求这些资源时,Nginx 可以直接从缓存中返回,减少对后端服务器的压力,加快页面加载速度。缓存机制可以有效提高网站的响应速度和用户体验。

工作原理

Nginx 采用事件驱动的异步非阻塞方式处理请求。它通过一个主进程和多个工作进程来处理网络连接和请求。主进程负责读取配置文件、初始化服务器环境、创建监听套接字等工作,而工作进程则负责处理具体的请求。在处理请求过程中,Nginx 采用高效的事件驱动机制,能够快速响应客户端请求,同时保持较低的资源消耗。

安装与配置

Nginx下载地址:nginx: download

根据自己的需求去下载对应的版本就好

下载完之后,可以双击启动也可以cmd指令启动,用指令的话如果启动不了报错是可以看见的双击的话报错都可能不知道,cmd指令就是再搜索栏打个cmd进入指令窗口后运行nginx.exe

一开始启动前肯定是需要修改他的默认配置的

找到conf文件中的nginx.conf配置文件

在其他地方都不需要管的

复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#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  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;
    #    }
    #}
	
    server {
        listen       9001;
        server_name  localhost;
        location ~ /acl/ {           
            proxy_pass http://localhost:8201;
        }      

        location ~ /sys/ {           
            proxy_pass http://localhost:8202;
        }
		
		location ~ /product/ {           
            proxy_pass http://localhost:8203;
        }
		
		location ~ /search/ {           
            proxy_pass http://localhost:8204;
        }
    }
}

就比如以我的配置为例吧,像我这个一共就是有四个端口,每个端口代表着不同的服务和功能

复制代码
server {
        listen       9001;
        server_name  localhost;
        location ~ /acl/ {           
            proxy_pass http://localhost:8201;
        }      

        location ~ /sys/ {           
            proxy_pass http://localhost:8202;
        }
		
		location ~ /product/ {           
            proxy_pass http://localhost:8203;
        }
		
		location ~ /search/ {           
            proxy_pass http://localhost:8204;
        }
    }

整体结构

这段配置代码位于一个server块内,定义了一个监听在9001端口的服务器。server_name被设置为localhost,这意味着该配置适用于本地主机访问。

具体配置解释

  • /acl/路径配置
  • 对于以/acl/开头的请求路径(location ~ /acl/),Nginx 将作为反向代理。
  • 它会将请求转发到http://localhost:8201。这里使用了proxy_pass指令来实现反向代理功能。
  • /sys/路径配置
  • 对于以/sys/开头的请求路径(location ~ /sys/),Nginx 会将请求转发到http://localhost:8202。
  • /product/路径配置
  • 对于以/product/开头的请求路径(location ~ /product/),Nginx 会将请求转发到http://localhost:8203。
  • /search/路径配置
  • 对于以/search/开头的请求路径(location ~ /search/),Nginx 会将请求转发到http://localhost:8204。

修改了配置文件的话需要在任务资源管理器去结束后重新启动

在项目中启动服务模块,可以看到有对应的端口号

总结

Nginx 作为一款功能强大的 Web 服务器软件,在网站搭建和性能优化方面发挥着重要作用。通过深入了解 Nginx 的核心概念、配置方法以及应用场景,我们可以充分利用 Nginx 的优势,构建高效、稳定、安全的网站。在实际应用中,不断优化配置和策略,根据具体需求灵活调整,以适应不断变化的网络环境。

相关推荐
玖石书6 小时前
Ubuntu 24.04 UFW 全命令攻略:从入门到端口开放实战
linux·运维·ubuntu
xiaoqiMikko6 小时前
Linux 实战(七):useradd、sudoers、umask、SGID、软链 —— 三台真机实测
linux·centos
拂拉氏8 小时前
【知识讲解】 Linux之动静态库的了解
linux·运维·服务器·动静态库
Fcy6488 小时前
Linux下 五种IO模型的认识与⾮阻塞IO的实现
linux·服务器·io
Mikko79 小时前
Linux 重启后日志没了?进程被 OOM 杀了去哪看?logrotate 之后日志还往旧文件写?三台真机实测日志与性能排查
linux·运维·centos
书生执笔画浮沉9 小时前
分享一个开源小工具CAdapt
linux·c·gnu·glibc·kernel·升级·灵感
光依旧9 小时前
发版就掉请求?Spring Boot优雅停机的四步
运维·spring boot·nginx·高可用·优雅停机·mcp server
mounter6259 小时前
Linux 内核热升级(Live Update)技术详解:PCI、VFIO 与 IOMMU 子系统的演进与实战解析引言
linux·linux kernel·kernel·iommu·vfio
砚凝霜10 小时前
【软考信息安全】第十九章 Unix/Linux安全分析与防护
linux·安全·unix
天国梦10 小时前
国密门禁品牌怎么选?2026年最新3个避坑要点
运维·服务器·数据库·python