环境说明
系统OS:CentOS7
IP: 192.168.1.7
配置过程
- 创建 /etc/yum.repos.d/nginx.repo 文件,向里面添加内容:
# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
-
执行yum安装
# yum install nginx -y
-
修改nginx配置文件,添加域名映射。
# vi /etc/nginx/nginx.conf
user root;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
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" "$request_time"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
fastcgi_buffer_size 1024k;
fastcgi_buffers 6 256k;
fastcgi_busy_buffers_size 1024k;
client_max_body_size 100m;
client_body_buffer_size 2048k;
proxy_max_temp_file_size 0;
proxy_buffering off;
proxy_connect_timeout 172800;
proxy_send_timeout 172800;
proxy_read_timeout 60;
#gzip on;
include /etc/nginx/conf.d/*.conf;
server{
listen 80;
server_name cas.dimonho.com; #将域名cas.dimonho.com映射到192.168.1.101的8080端口
location / {
proxy_pass http://192.168.1.101:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
}
server{
listen 80;
server_name spis.dimonho.com; #将域名spis.dimonho.com映射到192.168.1.101的18080端口
location / {
proxy_pass http://192.168.1.101:18080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
}
}
-
设置开机启动nginx服务
# systemctl enable nginx
-
启动nginx服务
# systemctl start nginx
-
设置防火墙允许80端口访问
# firewall-cmd --add-port=80/tcp --permanent
路由器NAT映射
- 访问192.168.1.1
- 路由器登陆:admin/admin123
-
虚拟NAT转发,添加NAT端口映射
NAT
配置域名解析
-
查看自己的公网IP
公网IP -
注册一个域名,添加域名解析
域名解析 -
当访问 http://cas.dimonho.com 或 http://spis.dimonho.com 时,DNS服务器解析该域名找到IP 175.8.210.184, http://175.8.210.184:80 通过路由器NAT转发到 http://192.168.1.7:80 请求nginx服务,由nginx将 cas.dimonho.com 和 spis.dimonho.com 分别解析为请求内部的 http://192.168.1.101:8080 和http://192.168.1.101:18080 返回结果。
至此,一套完整的nginx服务流程才算完成。
网友评论