使用nginx将域名指向服务 2021-03-24 16:04 > 前提是你有一台服务器,上面运行着一个服务,有一个域名。你想要将域名指到这台服务器的这个服务上。 # 设置域名解析 在域名服务控制台,添加解析记录,例:我的域名的riun.xyz,我想要使用minio.riun.xyz,那么需要添加记录,主机记录为minio,记录值设置为想要转发到的服务器ip  # 安装并配置nginx 1、安装包下载地址:http://nginx.org/download/nginx-1.16.1.tar.gz 2、将安装包上传至指定目录:`cd /home` `mkdir nginx` `cd nginx` `rz` 3、`yum install pcre-devel zlib-devel -y` `yum install -y gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel geoip-devel unzip patch` 4、解压:`tar -zxvf nginx-1.16.1.tar.gz` `cd nginx-1.16.1` 5、创建用户:`useradd -s /sbin/nologin www` 6、编译:`./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=www --group=www --with-http_ssl_module` 7、安装:`make && make install` 8、启动:`nginx` 9、修改配置文件:`vim /etc/nginx/nginx.conf` 在文件尾部,最后一个花括号之前添加上 `include /etc/nginx/conf.d/*.conf;` ,保存退出。 10、创建新的配置文件:`mkdir conf.d` `cd conf.d` `vim minio.conf` (这里的文件名minio可以是任意,一般写成你的应用名以便区分) 11、编辑配置文件,将域名指向服务:向minio.conf中写入 ```conf server { listen 80; server_name minio.riun.xyz; index default.htm index.html index.htm default.html; access_log /var/log/nginx/minio.access.log; error_log /var/log/nginx/minio.error.log; location / { proxy_pass http://127.0.0.1:9000/; proxy_read_timeout 300s; proxy_send_timeout 300s; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` listen 80; 代表监听本台服务器的80端口(本机的80端口要开放) server_name minio.riun.xyz; 代表处理minio.riun.xyz域名发过来的请求 proxy_pass http://127.0.0.1:9000/; 代表将这个请求发到本机的9000端口上 这样就能将 http://minio.riun.xyz 这样的请求发到本台服务器的9000端口上,然后你的9000端口要是你的服务端口,就能处理请求了。 12、重启:`cd /usr/sbin` `./nginx -s reload` (若找不到nginx命令,可以使用`whereis nginx`查看nginx在哪) 之后就可以通过 http://minio.riun.xyz 访问我目标服务器上9000端口的服务了。  --END--
发表评论