nginx笔记

windows下nginx教程

windows安装nginx

下载对应windows版本的nginx最新版本

下载链接:https://nginx.org/en/download.html

/images/nginx笔记/nginx_note_1.png

运行nginx

下载下来的是一个压缩包,进入解压出来的nginx目录内,然后在当前目录运行cmd,运行方法是在当前的目录的地址栏将路径删掉,然后输入cmd并回车

/images/nginx笔记/nginx_note_2.png

修改nginx配置文件

在修改nginx配置文件之前,先前nginx配置文件拷贝一份,这样如果nginx没有配置好还可以用原来的nginx配置文件恢复

nginx的配置文件在nginx解压目录的conf目录,文件名是 nginx.conf

/images/nginx笔记/nginx_note_3.png

打开配置文件查看内容

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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;
    #    }
    #}

}

准备工作

需要做的准备工作,在nginx的解压目录下新建一个myTest目录,再在myTest目录里建images子目录,然后存入图片,将图片命名为test2.png,也可以是其他命名,待会儿在链接里要访问的是test2.png

/images/nginx笔记/nginx_note_4.png

修改nginx配置文件内容

修改nginx配置文件内容如下

监听 8911 端口,通过 localhost 访问,将 / 映射到 myTest/images 下

此配置是nginx比较精简的配置,events相关的内容必须要有,否则重新加载nginx会报错,http块中的server块则是配置访问的资源了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  events {
    worker_connections  1024;
}
http {
    server {
        listen       8911;
        server_name  localhost;
		autoindex on;
        location / {
            root   myTest/images;
        }
    }
}

然后在cmd窗口重新加载nginx

/images/nginx笔记/nginx_note_5.png

打开浏览器访问 localhost:8911/test2.png

/images/nginx笔记/nginx_note_6.png

windows配置其他目录

windows默认nginx运行程序所在目录为根目录,如果要配置其他路径的目录,需要配置绝对路径,并且对于windows的路径中的反斜杠要经过转义处理才能使得访问成功

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  events {
    worker_connections  1024;
}
http {
    server {
        listen       8911;
        server_name  localhost;		
        location / {
			autoindex on;
            root   d:\\share\\nginx\\yenao_test\\images;
        }
    }
}

/images/nginx笔记/nginx_note_7.png

配置多个端口显示不同的内容

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  events {
    worker_connections  1024;
}
http {
    server {
        listen       8911;
        server_name  localhost;		
        location / {
			autoindex on;
            root   d:\\share\\nginx\\yenao_test\\images;
        }
    }
	server {
        listen       8910;
        server_name  localhost;		
        location / {
			autoindex on;
            root   myTest/images;
        }
    }
}

/images/nginx笔记/nginx_note_8.png

给nginx网页设置密码

设置密码需要 htpasswd,该工具依赖 apache2,因此需要下载 apache2

下载apache2

官网链接:https://httpd.apache.org/

下载链接:https://www.apachelounge.com/download/

下载解压 apache2 的压缩包,在 apache2 的 bin 目录可以看到 htpasswd 程序

给网页设置密码

在cmd窗口执行行命令,会在nginx目录下生成一个password文件,这行命令最后是设定的用户名叫 yenao,输入这行命令后回车,终端会提示创建并确认密码

1
  htpasswd -c d:/share/nginx/nginx-1.25.3/password yenao

查看生成密码的过程和生成的用户名和密码

/images/nginx笔记/nginx_note_9.png

修改或删除密码

删除用户和密码

-D 删除指定的用户

1
2
  htpasswd -D /usr/local/nginx/password username
  # -D 删除指定的用户
修改用户和密码
1
2
3
4
5
  htpasswd -D /usr/local/nginx/password username
  htpasswd -b /usr/local/nginx/password username passwd
  # -D 删除指定的用户
  # -b htpassswd命令行中一并输入用户名和密码而不是根据提示输入密码
  # -p htpassswd命令不对密码进行进行加密,即明文密码

在nginx的配置文件加入关于密码的配置

需要添加提示输入密码的信息和提供密码文件的路径

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  events {
    worker_connections  1024;
}
http {
    server {
        listen       8911;
        server_name  localhost;

		auth_basic "请输入用户名和密码";
		auth_basic_user_file d:\\share\\nginx\\nginx-1.25.3\\password;
		
        location / {
			autoindex on;
            root   d:\\share\\nginx\\yenao_test\\images;
        }
    }
	server {
        listen       8910;
        server_name  localhost;
        location / {
			autoindex on;
            root   myTest/images;
        }
    }
}
效果

/images/nginx笔记/nginx_note_10.png

给整个网站添加密码

给整个网站,跟添加网页密码的操作类似,只是需要将密码相关的配置放在http块下,而不是放在server块下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  events {
	  worker_connections  1024;
  }
  http {

	  auth_basic "请输入用户名和密码";
	  auth_basic_user_file d:\\share\\nginx\\nginx-1.25.3\\password;
		
	  server {
		  listen       8911;
		  server_name  localhost;
		
		  location / {
			  autoindex on;
			  root   d:\\share\\nginx\\yenao_test\\images;
		  }
	  }
	  server {
		  listen       8910;
		  server_name  localhost;
		  location / {
			  autoindex on;
			  root   myTest/images;
		  }
	  }
  }

最终配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  events {
	  worker_connections  1024;
  }

  http {
	  charset utf-8;
	  autoindex_exact_size off;
	  autoindex_localtime on;
	  auth_basic "请输入用户和密码";
	  auth_basic_user_file d:\\share\\nginx\\nginx-1.25.3\\password;
	  server {
		  listen       8911;
		  server_name  localhost;

		  location / {
			  autoindex on;
			  root   d:\\share\\nginx\\yenao_test\\images;
		  }
	  }
	  server {
		  listen       8910;
		  server_name  localhost;
		  location / {
			  autoindex on;
			  root   myTest/images;
		  }
	  }
  }

linux下nginx教程

linux安装nginx

1
  apt install nginx -y

运行nginx

1
  nginx

nginx修改配置后重新加载配置

1
  nginx -s reload

配置方式跟配置方法参考上边windows的内容

注意事项

配置完后重新加载失败,需要注意有没有端口占用,最简单粗暴的方式就是将所有的nginx的进程给kill掉

1
  ps aux | grep "nginx" | awk '{print $2}' | xargs kill -9

nginx访问页面 中文乱码 解决方案

server层添加如下配置:

1
charset utf-8;

也有人说server层和访问路径都要配置一下,可以自己尝试看看效果。

Licensed under CC BY-NC-SA 4.0