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 的优势,构建高效、稳定、安全的网站。在实际应用中,不断优化配置和策略,根据具体需求灵活调整,以适应不断变化的网络环境。

相关推荐
xuanzdhc2 小时前
Linux 基础IO
linux·运维·服务器
愚润求学2 小时前
【Linux】网络基础
linux·运维·网络
bantinghy3 小时前
Linux进程单例模式运行
linux·服务器·单例模式
小和尚同志4 小时前
29.4k!使用 1Panel 来管理你的服务器吧
linux·运维
帽儿山的枪手4 小时前
为什么Linux需要3种NAT地址转换?一探究竟
linux·网络协议·安全
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
shadon1789 天前
回答 如何通过inode client的SSLVPN登录之后,访问需要通过域名才能打开的服务
linux
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
小米里的大麦9 天前
014 Linux 2.6内核进程调度队列(了解)
linux·运维·驱动开发
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器