文章 > RedHat > Linux8 搭建nginx web服务器

Linux8 搭建nginx web服务器

泡杯长岛冰茶 · 2021-09-20 1493 RedHat

i.IT运维上分入门篇

a)【常用软件镜像站地址

b)【VMware Workstation pro下载与安装

c)【Redhat8.4安装& VM Workstation安装系统

d)【VMware Workstation虚拟机快照版本管理

e)【VMware Workstation pro网络配置

f)【Linux8常用网络配置命令汇总

g)【Linux 磁盘管理】  

ii.IT运维上分初级篇

a)【Linux8 搭建缓存DNS服务器

b)【Linux8 BIND搭建DNS高可用

c)【Linux8 搭建apache web服务器

d)【Linux8 搭建nginx web服务器

【正在更新】

e)【企业级虚拟化架构概述与兼容性查询】

f)【企业级虚拟化esxi安装】

g)【企业级虚拟化vCenter安装】



nginx 安装配置


1. nginx 安装

[root@localhost ~]# yum module list nginx
Updating Subscription Management repositories.
Unable to read consumer identity
Last metadata expiration check: 0:06:10 ago on Mon 20 Sep 2021 08:05:59 PM CST.
local_AppStream
Name                         Stream                         Profiles                         Summary                              
nginx                        1.14 [d]                       common [d]                       nginx webserver                      
nginx                        1.16                           common [d]                       nginx webserver                      
nginx                        1.18                           common [d]                       nginx webserver                      

[root@localhost ~]# yum module install nginx:1.18 -y
Updating Subscription Management repositories.

[root@localhost ~]# 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

1.1. 创建一个web 站点

创建一个容器目录与index文件
[root@localhost ~]# mkdir /software;echo "test nginx" >/software/index.html

[root@localhost software]# egrep -nv "^$|#" /etc/nginx/nginx.conf    
5:user nginx;
6:worker_processes auto;
7:error_log /var/log/nginx/error.log;
8:pid /run/nginx.pid;
11:include /usr/share/nginx/modules/*.conf;
13:events {
14:    worker_connections 1024;
15:}
17:http {
18:    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
19:                      '$status $body_bytes_sent "$http_referer" '
20:                      '"$http_user_agent" "$http_x_forwarded_for"';
22:    access_log  /var/log/nginx/access.log  main;
24:    sendfile            on;
25:    tcp_nopush          on;
26:    tcp_nodelay         on;
27:    keepalive_timeout   65;
28:    types_hash_max_size 4096;
30:    include             /etc/nginx/mime.types;
31:    default_type        application/octet-stream;
36:    include /etc/nginx/conf.d/*.conf;
38:    server {
39:        listen       80;
40:        listen       [::]:80;
41:        server_name  _;
42:        root         /usr/share/nginx/html;
45:        include /etc/nginx/default.d/*.conf;
47:        error_page 404 /404.html;
48:            location = /40x.html {
49:        }
51:        error_page 500 502 503 504 /50x.html;
52:            location = /50x.html {
53:        }
55:    }
56:     server {                                            ## 声名虚拟主机      
57:             listen 80;                                  ## 定义端口
58:             server_name www.yunbee.net;                 ## 虚拟主机名
59:             access_log /var/log/nginx/access.yumbee.net.log main;
60:             error_log /var/log/nginx/errors.yumbee.net.log ;
61:             location / {                                ## 声名根容器目录(可不写)
62:             root /software;                             ## 声名根容器目录位置
63:             index index.html;                           ## 声名主页文件
64:                             }                           ##
65:             }                                           ##
67:    }

1.1.1. 检查语法并开启服务

[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl enable nginx --now
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.


1.1.2.测试虚拟主机


[root@localhost ~]# curl www.yunbee.net
test nginx


1.2. 创建一个TLS web 站点

[root@localhost software]# egrep -nv "^$|#" /etc/nginx/nginx.conf 
5:user nginx;
6:worker_processes auto;
7:error_log /var/log/nginx/error.log;
8:pid /run/nginx.pid;
11:include /usr/share/nginx/modules/*.conf;
13:events {
14:    worker_connections 1024;
15:}
17:http {
18:    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
19:                      '$status $body_bytes_sent "$http_referer" '
20:                      '"$http_user_agent" "$http_x_forwarded_for"';
22:    access_log  /var/log/nginx/access.log  main;
24:    sendfile            on;
25:    tcp_nopush          on;
26:    tcp_nodelay         on;
27:    keepalive_timeout   65;
28:    types_hash_max_size 4096;
30:    include             /etc/nginx/mime.types;
31:    default_type        application/octet-stream;
36:    include /etc/nginx/conf.d/*.conf;
38:    server {
39:        listen       80;
40:        listen       [::]:80;
41:        server_name  _;
42:        root         /usr/share/nginx/html;
45:        include /etc/nginx/default.d/*.conf;
47:        error_page 404 /404.html;
48:            location = /40x.html {
49:        }
51:        error_page 500 502 503 504 /50x.html;
52:            location = /50x.html {
53:        }
55:    }
67:     server {
68:     listen 443 ssl;                   ## 声名端口与协议
69:     server_name www.yunbee.net;
70:     location /{
71:             root /software;
72:             index index.html;
73:     }
74:     ssl_certificate "/software/myserver.crt";       ## 声名证书位置
75:     ssl_certificate_key "/software/myserver.key";   ## 声名私钥位置
76:     }
82:    }


2.重定向tls


egrep -nv "^$|#" /etc/nginx/nginx.conf
5:user nginx;
6:worker_processes auto;
7:error_log /var/log/nginx/error.log;
8:pid /run/nginx.pid;
11:include /usr/share/nginx/modules/*.conf;
13:events {
14:    worker_connections 1024;
15:}
17:http {
18:    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
19:                      '$status $body_bytes_sent "$http_referer" '
20:                      '"$http_user_agent" "$http_x_forwarded_for"';
22:    access_log  /var/log/nginx/access.log  main;
24:    sendfile            on;
25:    tcp_nopush          on;
26:    tcp_nodelay         on;
27:    keepalive_timeout   65;
28:    types_hash_max_size 4096;
30:    include             /etc/nginx/mime.types;
31:    default_type        application/octet-stream;
36:    include /etc/nginx/conf.d/*.conf;
38:    server {
39:        listen       80;
40:        listen       [::]:80;
41:        server_name  _;
42:        root         /usr/share/nginx/html;
45:        include /etc/nginx/default.d/*.conf;
47:        error_page 404 /404.html;
48:            location = /40x.html {
49:        }
51:        error_page 500 502 503 504 /50x.html;
52:            location = /50x.html {
53:        }
55:    }
66:     server {
67:             listen 80;
68:             server_name www.yunbee.net;
69:             return 301 https://www.yunbee.net;     ## 重定向位置
70:             }
73:     server {
74:     listen 443 ssl;
75:     server_name www.yunbee.net;
76:     location /{
77:             root /software;
78:             index index.html;
79:     }
80:     ssl_certificate "/software/myserver.crt";
81:     ssl_certificate_key "/software/myserver.key";
82:    }
83:}

3.反向代理

worker_processes  1;
events {
   worker_connections  1024;
}
http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;
       upstream group1{                                     ##资源组group1
                       server 192.168.1.201:80 weight=50;   ##资源组group1成员1, weight权重为50
                       server 192.168.1.202:80 weight=50;   ##资源组group1成员2, weight权重为50
                       }
   server {
       listen       80;
       server_name  localhost;
       location / {
       proxy_pass "http://group1";                          ##proxy_pass反向代理到,资源组group1
       }
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
   }
}


本篇完!


点击加载更多