安装
centos
1、源码安装,不太建议。
wget http://nginx.org/download/nginx-1.13.9.tar.gz
tar -xzvf nginx-1.13.9.tar.gz
# 安装编译工具
sudo yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 编译并安装
cd nginx-1.13.9
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_v2_module
make && sudo make install
# 开机启动
sudo vim /etc/rc.d/init.d/autostart
# nginx
/usr/local/nginx/sbin/nginx
# 配置
vim /usr/local/nginx/conf/nginx.conf
# 快捷键
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
# 启动
/usr/local/nginx/sbin/nginx
# 停止
/usr/local/nginx/sbin/nginx -s stop
# 重写加载
/usr/local/nginx/sbin/nginx -s reload
2、repo安装,建议
参考:http://nginx.org/en/linux_packages.html#RHEL-CentOS
sudo vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
sudo yum -y install nginx
# 服务
sudo systemctl start nginx.service #启动nginx服务
sudo systemctl stop nginx.service #关闭nginx服务
sudo systemctl restart nginx.service #重启nginx服务
sudo systemctl status nginx.service #查看nginx服务运行状态
# 文件路径
1.nginx的配置文件在/etc/nginx/nginx.conf
2.自定义的配置文件放在/etc/nginx/conf.d
3.项目文件存放在/usr/share/nginx/html/
4.日志文件存放在/var/log/nginx/
5.还有一些其他的安装文件都在/etc/nginx
windows
1、下载:http://nginx.org/en/download.html
2、双击运行(可以在conf修改配置)
3、在任务管理器删除
配置
重定向+ssl
server {
listen 80;
server_name blog.lomot.cn;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name blog.lomot.cn;
# ssl
ssl_certificate /etc/nginx/ssl/blog.lomot.cn.cer;
ssl_certificate_key /etc/nginx/ssl/blog.lomot.cn.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# log files
access_log /var/www/wordpress/log/tianyunperfect.cn.access.log;
error_log /var/www/wordpress/log/tianyunperfect.cn.error.log;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
路由
规则
location [=|~|~*|^~] /uri/ {
...
}
=:精确匹配
^~:精确前缀匹配
~:区分大小写的正则匹配
~*:不区分大小写的正则匹配
/uri:普通前缀匹配
/:通用匹配
静态文件示例
location ^~ /img/ {
root /data/ ;
}
# 访问ip/img/123 会指向:/data/img/123
路由转发示例
upstream baize {
server 10.128.2.224:8100;
}
server {
listen 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root /home/medical/bussiness_service/frontend;
index index.html index.htm;
}
location ^~ /baize/ {
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://baize;
}
tomcat 负载均衡
upstream tomcat_server{
server 127.0.0.1:8080 weight=1;
}
server {
listen 80;
server_name localhost;
location ~ .*\.(html|js|css|ico|png|jpg|eot|svg|ttf|woff) {
root /usr/local/apache-tomcat-9.0.16/webapps ;
}
location / {
proxy_pass http://tomcat_server$request_uri;
}
}
支持websocket
如果是https,web端要把 ws://修改为 wss://
proxy_set_header Upgrade $http_upgrade; #需要添加这两行配置,让nginx响应websocket请求
proxy_set_header Connection "upgrade";
proxy_connect_timeout 4s; #为了防止socket链接很快就断掉需要按照各自的需求配置如下三个参数
proxy_read_timeout 7200s;
proxy_send_timeout 12s;
设置请求最大值
在location里面设置:
client_max_body_size 1000M;
参考文档
nginx中文文档:http://www.nginx.cn/doc/