Linux8 搭建nginx web服务器

nginx 安装配置。1.2.测试虚拟主机。2. 创建一个TLS web 站点。*禁止转载,可转发。TDPUB数+社区。

作者:泡杯长岛冰茶

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:}

本篇完!

*禁止转载,可转发(转发文章请注明出处)

TDPUB数+社区

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/10023.html

(0)
上一篇 2023年 4月 22日 下午11:55
下一篇 2023年 4月 22日 下午11:55

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信