WHAT WHY WHERe WHO + HOW
Nginx是什么?链接
和Apache的区别?链接1
链接2
参考1
[root@centos-a1 ~]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz /usr/share/man/man3/nginx.3pm.gz
[root@centos-a1 ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
说明:
- nginx配置文件目录:/etc/nginx/
- PID目录:/var/run/nginx.pid
- 错误日志:/var/log/nginx/error.log
- 访问日志:/var/log/nginx/access.log
- 默认站点目录:/usr/share/nginx/html
- 主配置文件路径:/etc/nginx/nginx.conf
- nginx的手册和帮助文件:/usr/share/
- nginx服务启动管理的终端命令:/usr/sbin/nginx
- ngxin-debug主要用于nginx 启动 debug 模式的一个命令:/usr/sbin/nginx-debug
- nginx 模块目录:/usr/lib64/nginx/modules/
链接1
正向代理与反向代理:通俗易懂(不严谨)
二、配置与管理Nginx服务器:1、Nginx是一款轻量级Web服务器,反向代理服务器
2、Nginx内存占用少,启动快,在互联网项目中广泛应用
3、Nginx专为性能优化而开发,高稳定、高性能、高效率,功能丰富,模块化结构,支持热部署,支持5w个并发连接数
4、Nginx直接支持Rails和PHP的程序,可作为负载均衡服务器以及邮件代理服务器,可帮助实现前端动静分离
5、正向代理和反向代理图例:
参考1
参考2
[root@centos-a1 ~]# yum install nginx -y Loaded plugins: fastestmirror, langpacks Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.njupt.edu.cn * updates: mirrors.aliyun.com base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 updates/7/x86_64/primary_db | 16 MB 00:00:21 No package nginx available. ----->报错 Error: Nothing to do
出现这个的原因是因为本地yum源中没有我们想要的nginx
参考1
解决方法之一:可以借助额外的第三方软件库epel拓展
EPEL(Extra Packages for Enterprise Linux),企业版Linux额外包,RHEL分布⾮标准包的社区类库
参考2
[root@centos-a1 ~]# yum install epel-release -y [root@centos-a1 ~]# yum install nginx -y `再次安装,就会发现yum源从epel库下载nginx`
`自启动,启动后可以通过IP地址或者域名访问`
[root@centos-a1 ~]# systemctl start nginx
[root@centos-a1 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
`检查服务`
[root@centos-a1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2022-06-09 15:41:39 CST; 1min 27s ago
Main PID: 57084 (nginx)
CGroup: /system.slice/nginx.service
├─57084 nginx: master process /usr/sbin/nginx
├─57085 nginx: worker process
└─57086 nginx: worker process
Jun 09 15:41:39 centos-a1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jun 09 15:41:39 centos-a1 nginx[57079]: nginx: the configuration file /etc/nginx/nginx.co... ok
Jun 09 15:41:39 centos-a1 nginx[57079]: nginx: configuration file /etc/nginx/nginx.conf t...ful
Jun 09 15:41:39 centos-a1 systemd[1]: Started The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@centos-a1 ~]# ps -ef | grep nginx
root 57084 1 0 15:41 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 57085 57084 0 15:41 ? 00:00:00 nginx: worker process
nginx 57086 57084 0 15:41 ? 00:00:00 nginx: worker process
root 57134 56732 0 15:43 pts/1 00:00:00 grep --color=auto nginx
2、配置主站和子站的主配文件及站点首页,测试浏览访问
参考
[root@centos7-a3 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.111.21 www.mmnl.edu
[root@centos-a1 html]# vim /etc/nginx/nginx.conf
server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } 查看主配文件可知主站目录存放于/usr/share/nginx/html;
[root@centos-a1 html]# cd /usr/share/nginx
[root@centos-a1 nginx]# ll
total 0
drwxr-xr-x 3 root root 136 Jun 9 15:40 html
drwxr-xr-x 2 root root 6 Oct 19 2021 modules
[root@centos-a1 nginx]# cd html/
[root@centos-a1 html]# ll
total 12
-rw-r--r-- 1 root root 3650 Oct 19 2021 404.html
-rw-r--r-- 1 root root 3693 Oct 19 2021 50x.html
lrwxrwxrwx 1 root root 20 Jun 9 15:40 en-US -> ../../doc/HTML/en-US
drwxr-xr-x 2 root root 27 Jun 9 15:40 icons
lrwxrwxrwx 1 root root 18 Jun 9 15:40 img -> ../../doc/HTML/img
lrwxrwxrwx 1 root root 25 Jun 9 15:40 index.html -> ../../doc/HTML/index.html
-rw-r--r-- 1 root root 368 Oct 19 2021 nginx-logo.png
lrwxrwxrwx 1 root root 14 Jun 9 15:40 poweredby.png -> nginx-logo.png
[root@centos-a1 html]# mv index.html index.html.bz
[root@centos-a1 html]# vim index.html
nginx home page
[root@centos7-a3 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.111.21 tea.mmnl.edu
192.168.111.21 www.mmnl.edu
[root@centos-a1 ~]# cd /etc/nginx
[root@centos-a1 nginx]# vim nginx.conf
36 include /etc/nginx/conf.d/*.conf;
37
38 server {
39 listen 80;
40 listen [::]:80;
41 server_name www.mmnl.edu;
42 root /usr/share/nginx/html;
43
44 # Load configuration files for the default server block.
45 include /etc/nginx/default.d/*.conf;
46
47 error_page 404 /404.html;
48 location = /404.html {
49 }
50
51 error_page 500 502 503 504 /50x.html;
52 location = /50x.html {
53 }
54 }
55
56 server {
57 listen 80;
58 listen [::]:80;
59 server_name tea.mmnl.edu;
60 root /usr/share/nginx/tea;
61
62 # Load configuration files for the default server block.
63 include /etc/nginx/default.d/*.conf;
64
65 error_page 404 /404.html;
66 location = /404.html {
67 }
68
69 error_page 500 502 503 504 /50x.html;
70 location = /50x.html {
71 }
72 }
73
[root@centos-a1 nginx]# cd /usr/share/nginx/
[root@centos-a1 nginx]# ll
total 0
drwxr-xr-x 3 root root 157 Jun 9 16:31 html
drwxr-xr-x 2 root root 6 Oct 19 2021 modules
[root@centos-a1 nginx]# mkdir tea
[root@centos-a1 nginx]# echo "tea home page" > /usr/share/nginx/tea/index.html
[root@centos-a1 nginx]# cat /usr/share/nginx/tea/index.html
tea home page
[root@centos-a1 nginx]# systemctl restart nginx
3、配置主站的反向代理,指向子站目标服务器,测试浏览访问
[root@centos-a1 nginx]# vim /etc/nginx/nginx.conf
38 server {
39 listen 80;
40 listen [::]:80;
41 server_name www.mmnl.edu;
42 root /usr/share/nginx/html;
43
44 # Load configuration files for the default server block.
45 include /etc/nginx/default.d/*.conf;
46
47 location / {
48 root html;
49 proxy_pass http://tea.mmnl.edu;
50 index index.html index.htm;
51 }
52
53 error_page 404 /404.html;
54 location = /404.html {
55 }
56
57 error_page 500 502 503 504 /50x.html;
58 location = /50x.html {
59 }
60 }
[root@centos-a1 nginx]# nginx -t
nginx: [emerg] host not found in upstream "tea.mmnl.edu" in /etc/nginx/nginx.conf:81
nginx: configuration file /etc/nginx/nginx.conf test failed
`报错是因为代理返回的https://tea.mmnl.edu是外网的域名,无法访问
所以我们需要把tea.mmnl.edu指向本地服务器`
[root@centos-a1 nginx]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 tea.mmnl.edu
`再次检查,无误`
[root@centos-a1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@centos-a1 nginx]# systemctl restart nginx
`使用客户端访问主站,成功指向子站`
4、分别配置基于主机、端口、域名的虚拟主机,测试确认
参考
`单网卡多IP的方式` [root@centos-a1 nginx]# ip addr add 192.168.111.101 dev ens33 [root@centos-a1 nginx]# ip addr add 192.168.111.102 dev ens33 [root@centos-a1 nginx]# ip addr 1: lo:mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:69:52:ae brd ff:ff:ff:ff:ff:ff inet 192.168.111.21/24 brd 192.168.111.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.111.101/32 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.111.102/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::4202:5ba2:2c88:691f/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: ens37: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:69:52:b8 brd ff:ff:ff:ff:ff:ff inet 192.168.1.1/24 brd 192.168.1.255 scope global noprefixroute ens37 valid_lft forever preferred_lft forever inet6 fe80::f5cc:342b:2e2b:4824/64 scope link noprefixroute valid_lft forever preferred_lft forever 4: virbr0: mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:06:05:6b brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 5: virbr0-nic: mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000 link/ether 52:54:00:06:05:6b brd ff:ff:ff:ff:ff:ff [root@centos-a1 nginx]# cd /usr/share/nginx/ [root@centos-a1 nginx]# ll total 0 drwxr-xr-x 3 root root 157 Jun 9 16:31 html drwxr-xr-x 2 root root 6 Oct 19 2021 modules drwxr-xr-x 2 root root 24 Jun 9 17:16 tea [root@centos-a1 nginx]# mkdir ip1 ip2 [root@centos-a1 nginx]# echo "ip1 home page" > /usr/share/nginx/ip1/index.html [root@centos-a1 nginx]# echo "ip2 home page" > /usr/share/nginx/ip2/index.html [root@centos-a1 nginx]# cat /usr/share/nginx/ip1/index.html ip1 home page [root@centos-a1 nginx]# vim /etc/nginx/nginx.conf server { listen 192.168.111.101:80; server_name ip1.mmnl.edu; root /usr/share/nginx/ip1; } server { listen 192.168.111.102:80; server_name ip2.mmnl.edu; root /usr/share/nginx/ip2; } [root@centos-a1 nginx]# nginx -t nginx: [warn] conflicting server name "www.mmnl.edu" on 0.0.0.0:80, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@centos-a1 nginx]# systemctl restart nginx [root@centos7-a3 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.111.21 www.mmnl.edu 192.168.111.101 ip1.mmnl.edu 192.168.111.102 ip2.mmnl.edu
`基于端口的虚拟主机`
[root@centos-a1 nginx]# vim /etc/nginx/nginx.conf
server {
listen 81;
server_name ip1.mmnl.edu;
root /usr/share/nginx/port1;
}
server {
listen 82;
server_name ip2.mmnl.edu;
root /usr/share/nginx/port2;
}
[root@centos-a1 nginx]# mkdir -p /usr/share/nginx/port1
[root@centos-a1 nginx]# mkdir -p /usr/share/nginx/port2
[root@centos-a1 nginx]# echo "port1 home page" > /usr/share/nginx/port1/index.html
[root@centos-a1 nginx]# echo "port2 home page" > /usr/share/nginx/port2/index.html
[root@centos-a1 nginx]# cat /usr/share/nginx/port1/index.html
port1 home page
[root@centos-a1 nginx]# systemctl restart nginx
[root@centos7-a3 ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.111.21 www.mmnl.edu
www.mmnl.edu:81 ip1.mmnl.edu
www.mmnl.edu:82 ip2.mmnl.edu
`基于域名的虚拟主机`
server {
listen 80;
server_name tea.mmnl.edu;
root /usr/share/nginx/tea;
}
server {
listen 80;
server_name stu.mmnl.edu;
root /usr/share/nginx/stu;
}
[root@centos-a1 nginx]# systemctl restart nginx
[root@centos-a1 nginx]# mkdir -p /usr/share/nginx/tea
[root@centos-a1 nginx]# mkdir -p /usr/share/nginx/stu
[root@centos-a1 nginx]# echo "tea home page" > /usr/share/nginx/tea/index.html
[root@centos-a1 nginx]# echo "stu home page" > /usr/share/nginx/stu/index.html
[root@centos-a1 nginx]# cat /usr/share/nginx/tea/index.html
tea home page
[root@centos7-a3 ~]# vim /etc/hosts
1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
3 192.168.111.21 www.mmnl.edu
4 www.mmnl.edu:81 ip1.mmnl.edu
5 www.mmnl.edu:82 ip2.mmnl.edu
6 192.168.111.21 tea.mmnl.edu
7 192.168.111.21 stu.mmnl.edu
参考
[root@centos-a1 ssl]# openssl genrsa -out server.key 1024
Generating RSA private key, 1024 bit long modulus
.......++++++
........++++++
e is 65537 (0x10001)
[root@centos-a1 ssl]# openssl rsa -in server.key -pubout -out server.pem
writing RSA key
[root@centos-a1 ssl]# openssl genrsa -out ca.key 1024
Generating RSA private key, 1024 bit long modulus
................++++++
..............................................++++++
e is 65537 (0x10001)
[root@centos-a1 ssl]# openssl genrsa -out client.key 1024
Generating RSA private key, 1024 bit long modulus
.............................++++++
.++++++
e is 65537 (0x10001)
[root@centos-a1 ssl]# openssl genrsa -out ca.key 1024
Generating RSA private key, 1024 bit long modulus
................++++++
..............................................++++++
e is 65537 (0x10001)
[root@centos-a1 ssl]# openssl req -new -key ca.key -out ca.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:hl95_ca
Organizational Unit Name (eg, section) []:hl95_sms_ca
Common Name (eg, your name or your server's hostname) []:192.168.111.21
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:192.168.111.21
[root@centos-a1 ssl]# openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=hl95_ca/OU=hl95_sms_ca/CN=192.168.111.21
Getting Private key
[root@centos-a1 ssl]# openssl req -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:hl95_server
Organizational Unit Name (eg, section) []:hl95_sms_server
Common Name (eg, your name or your server's hostname) []:192.168.111.21
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:192.168.111.21
[root@centos-a1 ssl]# openssl req -new -key client.key -out client.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:hl95_client
Organizational Unit Name (eg, section) []:hl95_sms_client
Common Name (eg, your name or your server's hostname) []:192.168.111.21
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:192.168.111.21
[root@centos-a1 ssl]# openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=hl95_server/OU=hl95_sms_server/CN=192.168.111.21
Getting CA Private Key
[root@centos-a1 ssl]# openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in client.csr -out client.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=hl95_client/OU=hl95_sms_client/CN=192.168.111.21
Getting CA Private Key
[root@centos-a1 ssl]# ll
total 48
-rw-r--r-- 1 root root 879 Jun 9 23:49 ca.crt
-rw-r--r-- 1 root root 741 Jun 9 23:48 ca.csr
-rw-r--r-- 1 root root 887 Jun 9 23:46 ca.key
-rw-r--r-- 1 root root 17 Jun 9 23:52 ca.srl
-rw-r--r-- 1 root root 891 Jun 9 23:52 client.crt
-rw-r--r-- 1 root root 749 Jun 9 23:51 client.csr
-rw-r--r-- 1 root root 891 Jun 9 23:46 client.key
-rw-r--r-- 1 root root 272 Jun 9 23:46 client.pem
-rw-r--r-- 1 root root 891 Jun 9 23:52 server.crt
-rw-r--r-- 1 root root 749 Jun 9 23:50 server.csr
-rw-r--r-- 1 root root 887 Jun 9 23:45 server.key
-rw-r--r-- 1 root root 272 Jun 9 23:45 server.pem
[root@centos-a1 ssl]# openssl rsa -in server.key -out server_nginx.key
writing RSA key
[root@centos-a1 ssl]# openssl x509 -req -days 3650 -in server.csr -signkey server_nginx.key -out server_nginx.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=hl95_server/OU=hl95_sms_server/CN=192.168.111.21
Getting Private key
38 server {
39 listen 8061 ssl;
40 server_name web.mmnl.edu;
41
42 ssl_certificate /usr/share/nginx/ssl/server_nginx.crt;
43 ssl_certificate_key /usr/share/nginx/ssl/server_nginx.key;
44
45 ssl_session_cache shared:SSL:1m;
46 ssl_session_timeout 5m;
47
48 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
49 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
50 ssl_prefer_server_ciphers on;
51
52 location / {
53
54 proxy_pass http://web.mmnl.edu;
55 proxy_set_header host $host;
56 proxy_set_header X-real-ip $remote_addr;
57 proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
58
59 }
60 }
[root@centos-a1 nginx]# nginx -t
nginx: [emerg] host not found in upstream "web.mmnl.edu" in /etc/nginx/nginx.conf:54
nginx: configuration file /etc/nginx/nginx.conf test failed
[root@centos-a1 nginx]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 tea.mmnl.edu
127.0.0.1 web.mmnl.edu
[root@centos-a1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@centos-a1 nginx]# systemctl restart nginx
HTTP转HTTPS三种方法



