nginx 简单使用方法

nginx是用于 Web 服务、反向代理、内容缓存、负载均衡、媒体流传输等场景的开源软件。

主要作用有三个:1、反向代理

  1. 负载均衡
  2. 动静分离

下载地址:nginx: download

nginx执行命令及启动

复制代码
//假设安装在E:\server\nginx-1.20.0目录下
//cmd命令进入安装文件
//启动
E:\server\nginx-1.20.0>start nginx
//或
E:\server\nginx-1.20.0>nginx.exe
//注意:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。


//停止
E:\server\nginx-1.20.0>nginx -s stop
或
E:\server\nginx-1.20.0>nginx -s quit
//注意:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息


//重启nginx
E:\server\nginx-1.20.0>nginx -s reload
//当配置信息修改,可以使用此命令重启nginx


//重新打开日志文件
E:\server\nginx-1.20.0>nginx -s reopen


//查看nginx版本
E:\server\nginx-1.20.0>nginx -v


//查看所有可执行的命令
E:\server\nginx-1.20.0>nginx -h

浏览器访问,出现如下页面表示nginx启动成功:

注意:如果命令行停止不了nginx,进入到电脑->任务管理器->进程(选中nginx右键鼠标结束任务)

nginx 代理、负载均衡和本地图片配置

复制代码
# 全局配置
#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 配置
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;
    
	# 负载均衡配置 - weight 数值越大防范概率越大
	upstream zdy {
		# 不可用服务器在30s内与服务端通讯成功2次,则认为服务器恢复
		server 127.0.0.1:8166 max_fails=2 fail_timeout=30s weight=1;
		server 127.0.0.1:8177 max_fails=2 fail_timeout=30s weight=2;
	}

    server {
        listen       81;
        server_name  127.0.0.1;
		
		# 项目访问负载均衡调用相应的端口
		location / {
			root html;
			index  index.html index.htm;
			# 反向代理
			proxy_pass http://zdy;
		}
		
		#配置访问images目录下的图片
        #图片访问地址:http://localhost:81/images/demo_1.jpg
        #前提本地D盘存在static文件夹,且里面存在demo_1.jpg图片
		location /images/ {
			alias D:/static/;
			autoindex on;  # 可选,允许目录列表
			access_log off;  # 可选,禁用访问日志记录
			expires 30d;  # 设置缓存时间
		}
		
	    # 默认
        #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 { # https 配置
    #    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;
    #    }
    #}

}
相关推荐
dhashdoia7 小时前
GPT-5.5 代码开发实战:Codex与Browser Use深度集成与星链4SAPI优化方案
java·数据库·人工智能·gpt·架构
xuhaoyu_cpp_java7 小时前
SpringMVC学习(二)
java·经验分享·笔记·学习·spring
笑洋仟8 小时前
docker的overlay2目录占用磁盘空间很大,清理办法
运维·docker·容器
木雷坞8 小时前
2026 年 5 月国内可用 Docker 镜像源列表与配置方法
运维·docker·容器
2301_780789669 小时前
“数字珍珠港”再现:西北能源基地DNS篡改事件深度复盘与防护升级
运维·服务器·网络·tcp/ip·网络安全·智能路由器·能源
TAN-90°-9 小时前
Java 3——getter和setter super()关键字
java·开发语言
wand codemonkey9 小时前
(二十七)Maven(依赖)【安装】+【项目结构】
java·开发语言·maven
linda公馆9 小时前
Maven项目报错:java:错误:不支持发行版本 5
java·开发语言·maven
老王谈企服9 小时前
2026制造业供应链韧性提升,智能化将成为核心解决方案吗?基于实在Agent的端到端自动化实践
运维·人工智能·ai·自动化
Agent手记9 小时前
工厂货物智能入库全流程自动化:基于实在Agent与ISSUT技术的2026工业自动化实战指南
运维·人工智能·ai·自动化