Nginx 基础配置指南

3,149 阅读1分钟

1、 nginx 虚拟主机配置

server{
	listen 80;  #监听80端口
	server_name yqchen.cn; #域名地址
	location / {
		root 站点根目录  #例:/web/html
		index 索引文件   #例: index.html
	}
}

2、 nginx 反向代理配置

server{
	listen 80; #监听80端口
	server_name yqchen.cn  #域名地址;
	location / { #匹配路径
		proxy_pass  http://localhost:3000; #转发地址
	}
}

3、 nginx SSL证书安装开启https并将http请求重写成https请求

  • 1、上传证书至服务器

  • 2、修改nginx.conf配置文件


server{
	listen 443;  #监听443端口
    server_name yqchen.cn; 
    ssl on; #打开ssl
    ssl_certificate  ssl/www.yqchen.cn_bundle.crt;  #公钥 crt证书的名字
    ssl_certificate_key ssl/www.yqchen.cn.key;      #私钥 key证书的名字
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;            #三种不同的协议
	location / { #匹配路径
		root 站点根目录  #例:/web/html
		index 索引文件   #例: index.html
	}
}
#将http请求重写成https请求
server{
    listen 80;#监听80端口
    server_name yqchen.cn; #你的域名
    rewrite ^(.*)$ https://yqchen.cn  permanent; #把http的域名请求转成https
}

4、nginx 常用命令

   nginx -t  #检查nginx配置文件是否配置正确   successful表示配置可用
   nginx -s reload #重新加载配置文件使配置生效
   nginx -s stop #停止nginx
   service nginx stop     // 停止nginx服务
   service nginx start    // 启动nginx服务
   service nginx restart  // 重启nginx服务