nginx多站点配置

Posted allen167

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx多站点配置相关的知识,希望对你有一定的参考价值。

一、安装nginx

https://yq.aliyun.com/articles/101144?spm=5176.10695662.1996646101.searchclickresult.70af967bColksb

二、nginx.conf文件配置

 1 user  root;
 2 worker_processes  1;
 3 
 4 #error_log  logs/error.log;
 5 #error_log  logs/error.log  notice;
 6 #error_log  logs/error.log  info;
 7 
 8 #pid        logs/nginx.pid;
 9 
10 
11 events {
12     worker_connections  1024;
13 }
14 
15 error_log          /usr/local/nginx/logs/error.log debug;
16 
17 http {
18     include       mime.types;
19     default_type  application/octet-stream;
20     
21    underscores_in_headers on;
22        log_format log_req_resp $remote_addr - $remote_user [$time_local] 
23         "$request" $status $body_bytes_sent 
24         "$http_referer" "$http_user_agent" $request_time 
25      req_header:"$request_body" req_body:"$request_body";
26 
27 
28     #access_log  logs/access.log  main;
29 
30     sendfile        on;
31     #tcp_nopush     on;
32 
33 
34     keepalive_timeout  65;
35    
36     #gzip  on;
37 
38     include /usr/local/nginx/conf/conf.d/*.conf;
39  
40 }

/usr/local/nginx/conf/conf.d/shout.conf配置

 1 upstream toshou.top{ 
 2         server blog.toshout.top; 
 3 }
 4 server {
 5     listen       80;
 6     server_name  toshou.top;
 7 
 8     #charset koi8-r;
 9     #access_log  /var/log/nginx/log/host.access.log  main;
10     location / {
11         proxy_pass http://toshou.top;
12         proxy_http_version 1.1;
13         proxy_set_header Host $host;
14         proxy_set_header X-Real-IP $remote_addr;
15         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
16         proxy_set_header Via "nginx";
17     }
18 
19     # redirect server error pages to the static page /50x.html
20     #
21     error_page   500 502 503 504  /50x.html;
22     location = /50x.html {
23         root   /usr/share/nginx/html;
24     }
25 }

绑定域名

shou.top域名绑定到nginx服务ip,访问toshou.top会把请求转发到blog.toshout.top

依此配置,使不同域名绑定nginx服务器ip,并创建子配置文件即可。

https配置

由于本人使用的是阿里云服务器,CA证书也是在阿里云购买的免费证书。

1、下载证书

2、上传证书至/usr/local/nginx/cert目录

3、新建配置文件toshout.top_ssl.conf

 1 server {
 2     listen       443;
 3     server_name  toshout.top;
 4     ssl          on;
 5 
 6     ssl_certificate      /usr/local/nginx/cert/weixin.daq.pem;
 7     ssl_certificate_key  /usr/local/nginx/cert/weixin.daq.key;
 8 
 9     location / {
10         proxy_pass http://blog.toshout.top;
11         proxy_http_version 1.1;
12         proxy_set_header Host $host;
13         proxy_set_header X-Real-IP $remote_addr;
14         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
15         proxy_set_header Via "nginx";
16     }
17 }

 

一、安装nginx

https://yq.aliyun.com/articles/101144?spm=5176.10695662.1996646101.searchclickresult.70af967bColksb

二、nginx.conf文件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

user root;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}

error_log /usr/local/nginx/logs/error.log debug;

http {
include mime.types;
default_type application/octet-stream;

underscores_in_headers on;
log_format log_req_resp ‘$remote_addr - $remote_user [$time_local] ‘
‘"$request" $status $body_bytes_sent ‘
‘"$http_referer" "$http_user_agent" $request_time‘
‘req_header:"$request_body" req_body:"$request_body"‘;


#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;


keepalive_timeout 65;

#gzip on;

include /usr/local/nginx/conf/conf.d/*.conf;

}

/usr/local/nginx/conf/conf.d/shout.conf配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
upstream toshou.top{ 
server blog.toshout.top;
}
server {
listen 80;
server_name toshou.top;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_pass http://toshou.top;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

绑定域名

shou.top域名绑定到nginx服务ip,访问toshou.top会把请求转发到blog.toshout.top

依此配置,使不同域名绑定nginx服务器ip,并创建子配置文件即可。

https配置

由于本人使用的是阿里云服务器,CA证书也是在阿里云购买的免费证书。

1、下载证书

2、上传证书至/usr/local/nginx/cert目录

3、新建配置文件toshout.top_ssl.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 443;
server_name toshout.top;
ssl on;

ssl_certificate /usr/local/nginx/cert/weixin.daq.pem;
ssl_certificate_key /usr/local/nginx/cert/weixin.daq.key;

location / {
proxy_pass http://blog.toshout.top;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Via "nginx";
}
}
































































































































































以上是关于nginx多站点配置的主要内容,如果未能解决你的问题,请参考以下文章

nginx下配置多站点

phpstudy使用nginx配置多站点域名

Ubuntu Nginx 配置多站点

nginx多站点配置

Nginx 多站点配置

Nginx 配置多站点vhost