1. 程式人生 > >Linux 下使用 Nginx 配置子域名

Linux 下使用 Nginx 配置子域名

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

下載並配置 Nginx

yum install nginx
vim /etc/nginx/nginx.cfg

配置示例:

# 下面有中文註釋的地方是重點
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