Linux 下使用 Nginx 配置子域名

使用 Nginx 的反向代理来解析子域名

下载并配置 Nginx

yum install nginx
vim /etc/nginx/nginx.conf

配置示例:

# 下面有中文注释的地方是重点
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;

    # 一个 server 就是一个解析
    server {
        listen       80 default_server;				#  要监听的端口,一般设置为 80
        listen       [::]:80 default_server;			
        server_name  itscloudy.cn www.itscloudy.cn;		# 自定义的子域名 + 域名,以空格间隔多个
        charset utf8;

        location / {
            proxy_pass       http://0.0.0.0:8888;		# 指向的服务(这里是使用 8888 端口的服务)
            proxy_set_header Host      $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

启动

nginx -c /usr/local/nginx/conf/nginx.conf

DONE

你可能感兴趣的:(Linux,Nginx,反向代理,Centos,子域名)