Ubuntu下nginx安装使用

Catalogue
  1. Nginx 配置多站点vhost
  2. Nginx 映射规则配置
  3. Nginx 负载均衡配置
  4. Nginx 配置文件详解
1
2
3
4
# 默认的nginx配置
cat /etc/nginx/sites-available/default

# 其中可以找到. 静态资源路径.root /var/www/html;

Nginx 配置多站点vhost

1.首先保证 /etc/nginx/nginx.conf 中存在以下配置,确保可以多配置.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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_forwa rded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;

include /etc/nginx/conf.d/*.conf;   #主要是加入此行,如有则忽略
}

2.在 /etc/nginx/conf.d/ 文件夹下添加不同的配置.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;
server_name wiki.quartz.ren;
access_log /var/log/wiki.access.log;
location / {
proxy_pass http://localhost:4000;
index index.html index.htm;
}

error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 注: 这里的转发可以是转发到端口,也可以转发到静态资源.

相应的添加其他配置文件.

3.在/etc/hosts添加域名映射. !!! 重要

1
2
3
127.0.0.1       wiki.quartz.ren
127.0.0.1 www.quartz.ren
127.0.0.1 santa.quartz.ren

大功告成~

Nginx 映射规则配置

Nginx 负载均衡配置

Nginx 配置文件详解