安装
yum install -y gcc gcc-c++ && cd /usr/local/ && wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz && tar -zxvf pcre-8.33.tar.gz && cd pcre-8.33 && ./configure && make && make install && cd /usr/local/ && wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1j.tar.gz && tar -zxvf openssl-1.0.1j.tar.gz && cd openssl-1.0.1j && ./config && make && make install && cd /usr/local/ && wget http://zlib.net/zlib-1.2.11.tar.gz && tar -zxvf zlib-1.2.11.tar.gz && ./configure && make && make install && cd /usr/local/ && wget http://nginx.org/download/nginx-1.8.0.tar.gz && tar -zxvf nginx-1.8.0.tar.gz && cd nginx-1.8.0 && ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.33 --with-zlib=/usr/local/zlib-1.2.11 && make && make install
配置域名和端口
在域名网站,将某域名解析到指定的的服务器IP
这一步在域名管理端完成
建立配置文件夹
在 /usr/local/nginx/conf 文件夹下建立 conf.d文件夹,如下图所示
编辑nginx.conf
nginx.conf的路径一般为/usr/local/nginx/conf
在nginx.conf文件中添加include conf.d/*.conf;意思是监听conf.d下的所有conf文件。
events {
worker_connections 1024;
}
http {
include mime.types;
include conf.d/*.conf;
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 logs/access.log main;
添加一级解析
在conf.d文件夹中新建并编辑mecube1.conf文件
事先已经将*.mecube.tyio.net解析到了47.113.186.9,nginx默认会监听80端口。
如果我们想让域名mecube.tyio.net直接访问到8000端口而不是80,则要在mecube.conf里面配置好如下, 该配置的意思是:
- 监听80端口
- 并将请求发送给本地的8000端口
server {
server_name mecube.tyio.net;
listen 80;
# for websocket, do not ignore any line
proxy_http_version 1.1;
proxy_connect_timeout 2;
proxy_read_timeout 300;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
添加二级解析
如要添加 mands.mecube.tyio.net到47.113.186.9:14003,只需要再建立一个mands.conf的文件,内容如下
server {
server_name mands.mecube.tyio.net;
listen 80;
# for websocket, do not ignore any line
proxy_http_version 1.1;
proxy_connect_timeout 2;
proxy_read_timeout 300;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
location / {
proxy_pass http://127.0.0.1:14003;
}
}
- 将mands.conf文件放到conf.d文件夹下
- 重启nginx,即可生效
nginx -s reload
常用操作
启动
/usr/local/nginx/sbin/nginx
重启
/usr/local/nginx/sbin/nginx –s reload
停止
/usr/local/nginx/sbin/nginx –s stop
强制关闭
pkill nginx