nginx快速上手(含一个详细案例)
nginx配置方法其实很简单
nginx配置文件核心结构:
http {
server {
listen ;
server_name ;
location 路径 {
proxy_pass
}
}
server {
listen ;
server_name ;
location 路径 {
proxy_pass
}
}
server {
listen ;
server_name ;
location 路径 {
proxy_pass
}
}
....
}
我们要修改和添加的,其实就是每一个server
项
对于一个server
,我们只需要关注3个参数:
listen
: 你要监听的端口号server_name
: 你要解析哪个域名发来的请求location
: 你要把这个请求转发到哪里
一个实际案例
需求:已经购买了一个域名xxx.com
,服务器地址y.y.y.y
(致敬y总)。想要用域名访问8080
端口下的springboot项目1
和运行在8000
端口下的django项目二
,操作如下:
1.首先进入阿里云域名控制台
2.点击添加记录
3.配置域名记录
4.配置nginx.conf
假设我们添加了2条记录,我们就有了2个2级域名:
game.xxx.com
spring.xxx.com
进入nginx配置文件
vim /etc/nginx/nginx.conf
原生的nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
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;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
添加的配置
要添加配置,其实就是添加2个server
server {
listen 80;
server_name game.xxx.com;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
server {
listen 80;
server_name spring.xxx.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
就是这么简单
完整的nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
http {
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;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name game.xxx.com;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
server {
listen 80;
server_name java.xxx.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
}
创作不宜,如要转载,请私聊作者个人空间 - AcWing
完结撒花~
每个二级域名如果都要支持https协议的话每个是不是都得申请一个ssl证书
我没试过,我猜应该是的