nginx一个端口配置多域名服务_第1张图片

本文演示如何在一个端口上配置多个域名。

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
#通用的内容可以放在http层配置。
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;

#配置https服务
server {
listen 443 ssl;
server_name www.mydomain.net;
root /var/www/html;

ssl_certificate /etc/nginx/cert/pem220223.pem;
ssl_certificate_key /etc/nginx/cert/key220223.key;
ssl_session_timeout 5m;

ssi on;
ssi_silent_errors on;
ssi_types text/shtml;

location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

#默认的http服务
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;

ssi on;
#ssi_silent_errors on;
ssi_types text/shtml;
# Load configuration files for the default server block.

location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

#配置一个http服务
server {
listen 80;
server_name sub1.mydomain.net; #多个域名使用空格隔开,或者使用通配符
root /var/www/html2;
index index.php index.html index.htm;

ssi on;
#ssi_silent_errors on;
ssi_types text/shtml;
# Load configuration files for the default server block.

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

error_page 404 /404.html;
location = /404.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

#还可以添加更多域名
}