linux centos搭建nginx并配置二级域名解析

由于公司只有一个公网IP,但是又想通过www.xxxx.com、git.xxxx.com、oa.xxxx.com分别访问搭载在一台服务器上的网站服务,但是只有一个80端口,无法达到公司想要的效果,这时候就需要使用nginx做二级域名转发。

配置方法

一、安装nginx

安装nginx并设置开机启动

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install -y nginx
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

另外可以修改nginx默认监听端口,nginx 默认端口为80,如果需要修改,找/etc/nginx/nginx.conf文件打开并切修改linsten

server {
        listen       80; ...

二、配置二级域名转发

打开/etc/nginx/nginx.conf文件,并且新增二级域名转发规则

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  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    index   index.html index.htm;

    #新增gitlab二级域名转发规则
    server {
          listen 81;
          server_name gitlab.xxxx.cn;

          location / {
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  Host   $http_host;
            proxy_pass     http://0.0.0.0:10101;
          }
        }
    #新增oa二级域名转发规则
        server {
          listen 81;
          server_name oa.xxxx.cn;

          location / {
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  Host   $http_host;
            proxy_pass     http://0.0.0.0:80;
          }
        }
    #新增www二级域名转发规则
        server {
          listen 81;
          server_name www.xxxx.cn;

          location / {
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  Host   $http_host;
            proxy_pass     http://192.168.0.222:80;
          }
        }
    #这是原有的服务器配置
    server {
        listen       81;
        server_name  localhost;
        root         /usr/share/nginx/html;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        location / {
        }
        省略...
        }
}

 

你可能感兴趣的