Nginx build
I. Overview:
Nginx是一款轻量级的Web服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在现在互联网项目中被广泛应用。
download link:
2. Prerequisites:
以下Nginx搭建是基于docker装好的情况下,所以须在Linux,Windows, MacOS等操作系统中安装好docker;
How to build a docker container ;
3. Installation:
1. Check whether docker has installed nginx (check whether the Nginx image exists)
docker images
2. Install nginx
docker pull nginx
3. Run the nginx mirror
docker run --name mynginx -p 80:80 -d nginx
Note: The meaning of –name, -p, -d and other operations can refer to:
How to build a docker container ;
4. View the running results
docker ps
5. Web page access, browser input virtual machine IP: port number address and press Enter, you can see "Welcome to nginx!"
4. Use Nginx
1. Open the port of centos and set:
Note: If it is a purchased cloud server, you can operate the whitelist and security group through console commands;
firewall-cmd --zone=public --list-ports #查看所有开放的端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent # 开放8080端口
firewall-cmd --zone=public --remove-port=8080/tcp --permanent #关闭8080端口
firewall-cmd --reload # 配置立即生效
systemctl start/stop/restart firewalld.service #打开/关闭/重启防火墙
firewall-cmd --state #查看防火墙状态
2. Create a management directory locally:
mkdir -p /data/nginx
mkdir -p /data/nginx/www
mkdir -p /data/nginx/conf
mkdir -p /data/nginx/logs
3. Copy the corresponding files in the container to the newly created management directory for container mapping:
docker cp 749eec3af5f8:/etc/nginx/nginx.conf /data/nginx/
docker cp 749eec3af5f8:/etc/nginx/conf.d /data/nginx/conf/
docker cp 749eec3af5f8:/usr/share/nginx/html/ /data/nginx/www/
docker cp 749eec3af5f8:/var/log/nginx/ /data/nginx/logs/
4. Stop and remove the container (use either id or name):
docker stop 749eec3af5f8 #停止容器
docker rm 749eec3af5f8 #移除容器
5. Nginx configuration - nginx.conf file modification (modify according to your actual requirements):
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
accept_mutex on;
multi_accept on;
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#upstream mynginx{
# server 10.0.1.3:8088 weight=1;
#}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 容器中的html静态文件地址
root /usr/share/nginx/html/html;
#proxy_pass http://mynginx;
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
#listen [::]:80;
# 设置自己的域名服务名称
server_name www.xxx.cn;
# rewrite ^(.*) https://$server_name$1 permanent;
# 把http的域名请求转成https
return 301 https://www.xxx.cn$request_uri;
}
server {
# ssl 443 端口
listen 443 ssl;
# 设置自己的域名
server_name www.xxx.cn;
# ssl配置文件-需要解析域名通过后下载Nginx版本文件
ssl_certificate /etc/nginx/ssl/xxx.cn_bundle.crt;
# ssl密钥文件-需要解析域名通过后下载Nginx版本文件
ssl_certificate_key /etc/nginx/ssl/xxx.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root /usr/share/nginx/html/html/PlayPlane-master;
index index.html index.htm;
}
}
include /etc/nginx/conf.d/*.conf;
}
Notice:
The domain name needs to go to Tencent Cloud, Alibaba Cloud, Wanwang, etc. to apply for the domain name;
1. Buy a Domain Name
2. Resolve the domain name
3. Domain name filing ( if the domain name is not filed, it will be detected, making the domain name unusable later )
6. Start the container again and mount the directory:
docker run --name nginx -p 80:80 -p 8088:8088 -p 443:443
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf
-v /data/nginx/www/html/:/usr/share/nginx/html/html/
-v /data/nginx/logs/:/var/log/nginx/
-v /data/nginx/conf/:/etc/nginx/conf.d
-v /etc/ssl/nginx/:/etc/nginx/ssl/
--privileged = true
-d nginx