Windows 环境配置 Nginx

部署

第一步

  从官网下载 Nginx 包,版本自己选择,官网地址:http://nginx.org/

第二步

  选择 Windows 版本的安装包进行下载,根据图片描述,最新版是 1.19.3 (具体看当前官网),稳定版是 1.18.0 (具体看当前官网)

第三步

  下载后得到的 exe 文件不要双击,用解压程序进行解压,得到一个文件夹,将文件夹复制到指定目录就完成了安装

注意:一定不要直接双击nginx.exe,这样会导致修改配置后重启、停止 nginx 无效,需要手动关闭任务管理器内的所有 nginx 进程,再启动才可以

第四步

  启动 Nginx

# 进入 Nginx 所在目录
cd c:
ginx-1.xx.x

# 启动 Nginx 服务
start nginx  # 执行后会有一闪而过的信息,是正常的

# 查看任务进程是否存在,可使用以下命令s或打开任务管理器都行
tasklist /fi "imagename eq nginx.exe"

# 日志路径 c:
ginx-1.xx.xlog

第五步

  浏览器访问 127.0.0.1:80,查看是否成功,看到以下页面表示成功

文档结构

默认配置文件路径 -- > c:/nginx-1.xx.x/conf/nginx.conf

#user  nobody;
# ---------- 工作进程数,一般设置为cpu核心数
worker_processes  1;

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

#pid        logs/nginx.pid;

# ---------- 最大连接数,一般设置为cpu*2048
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节点时,默认server names的缓存区大小就不够了,需要手动设置大一点
    server_names_hash_bucket_size 512

    # ---------- server表示虚拟主机可以理解为一个站点,可以配置多个server节点搭建多个站点,每一个请求进来确定使用哪个server由server_name确定
    server {
        listen       80;
        server_name  localhost;

        # ---------- 编码格式,避免url参数乱码可修改为charset utf-8
        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        # ----------location用来匹配同一域名下多个URI的访问规则,比如动态资源如何跳转,静态资源如何跳转等。location后面跟着的/代表匹配规则
        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;
    #    }
    #}

}
nginx.conf

注意事项

  1. Nginx 默认端口号为 80,若已被占用则无法启动,可在配置文件中修改端口号,修改陪之后可执行命令检查配置是否合法
    nginx -t -c /nginx-1.xx.x/conf/nginx.conf
  2. Nginx 文件夹路径不要包含中文

其他

  NGINX 相关命令

                   

             

原文地址:https://www.cnblogs.com/zhuminghui/p/13786770.html