安装

centos

1、源码安装,不太建议。

  1. wget http://nginx.org/download/nginx-1.13.9.tar.gz
  2. tar -xzvf nginx-1.13.9.tar.gz
  3. # 安装编译工具
  4. sudo yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  5. # 编译并安装
  6. cd nginx-1.13.9
  7. ./configure \
  8. --prefix=/usr/local/nginx \
  9. --with-http_stub_status_module \
  10. --with-http_ssl_module \
  11. --with-http_realip_module \
  12. --with-http_v2_module
  13. make && sudo make install
  14. # 开机启动
  15. sudo vim /etc/rc.d/init.d/autostart
  16. # nginx
  17. /usr/local/nginx/sbin/nginx
  18. # 配置
  19. vim /usr/local/nginx/conf/nginx.conf
  20. # 快捷键
  21. sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
  22. # 启动
  23. /usr/local/nginx/sbin/nginx
  24. # 停止
  25. /usr/local/nginx/sbin/nginx -s stop
  26. # 重写加载
  27. /usr/local/nginx/sbin/nginx -s reload

2、repo安装,建议

参考:http://nginx.org/en/linux_packages.html#RHEL-CentOS

  1. sudo vim /etc/yum.repos.d/nginx.repo
  2. [nginx-stable]
  3. name=nginx stable repo
  4. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  5. gpgcheck=1
  6. enabled=1
  7. gpgkey=https://nginx.org/keys/nginx_signing.key
  8. sudo yum -y install nginx
  9. # 服务
  10. sudo systemctl start nginx.service #启动nginx服务
  11. sudo systemctl stop nginx.service #关闭nginx服务
  12. sudo systemctl restart nginx.service #重启nginx服务
  13. sudo systemctl status nginx.service #查看nginx服务运行状态
  14. # 文件路径
  15. 1.nginx的配置文件在/etc/nginx/nginx.conf
  16. 2.自定义的配置文件放在/etc/nginx/conf.d
  17. 3.项目文件存放在/usr/share/nginx/html/
  18. 4.日志文件存放在/var/log/nginx/
  19. 5.还有一些其他的安装文件都在/etc/nginx

windows

1、下载:http://nginx.org/en/download.html

2、双击运行(可以在conf修改配置)

image.png
3、在任务管理器删除

image.png

配置

重定向+ssl

  1. server {
  2. listen 80;
  3. server_name blog.lomot.cn;
  4. return 301 https://$host$request_uri;
  5. }
  6. server {
  7. listen 443 ssl http2;
  8. server_name blog.lomot.cn;
  9. # ssl
  10. ssl_certificate /etc/nginx/ssl/blog.lomot.cn.cer;
  11. ssl_certificate_key /etc/nginx/ssl/blog.lomot.cn.key;
  12. ssl_session_timeout 5m;
  13. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  14. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
  15. ssl_prefer_server_ciphers on;
  16. # log files
  17. access_log /var/www/wordpress/log/tianyunperfect.cn.access.log;
  18. error_log /var/www/wordpress/log/tianyunperfect.cn.error.log;
  19. location / {
  20. proxy_pass http://localhost:3000;
  21. proxy_set_header X-Real-IP $remote_addr;
  22. proxy_set_header Host $http_host;
  23. proxy_set_header REMOTE-HOST $remote_addr;
  24. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25. }
  26. }

路由

规则

  1. location [=|~|~*|^~] /uri/ {
  2. ...
  3. }
  4. =:精确匹配
  5. ^~:精确前缀匹配
  6. ~:区分大小写的正则匹配
  7. ~*:不区分大小写的正则匹配
  8. /uri:普通前缀匹配
  9. /:通用匹配

静态文件示例

  1. location ^~ /img/ {
  2. root /data/ ;
  3. }
  4. # 访问ip/img/123 会指向:/data/img/123

路由转发示例

  1. upstream baize {
  2. server 10.128.2.224:8100;
  3. }
  4. server {
  5. listen 8088;
  6. server_name localhost;
  7. #charset koi8-r;
  8. #access_log logs/host.access.log main;
  9. location / {
  10. #root html;
  11. root /home/medical/bussiness_service/frontend;
  12. index index.html index.htm;
  13. }
  14. location ^~ /baize/ {
  15. proxy_set_header Host $host;
  16. proxy_set_header X-Real-Ip $remote_addr;
  17. proxy_set_header X-Forwarded-For $remote_addr;
  18. proxy_pass http://baize;
  19. }

tomcat 负载均衡

  1. upstream tomcat_server{
  2. server 127.0.0.1:8080 weight=1;
  3. }
  4. server {
  5. listen 80;
  6. server_name localhost;
  7. location ~ .*\.(html|js|css|ico|png|jpg|eot|svg|ttf|woff) {
  8. root /usr/local/apache-tomcat-9.0.16/webapps ;
  9. }
  10. location / {
  11. proxy_pass http://tomcat_server$request_uri;
  12. }
  13. }

支持websocket

如果是https,web端要把 ws://修改为 wss://

  1. proxy_set_header Upgrade $http_upgrade; #需要添加这两行配置,让nginx响应websocket请求
  2. proxy_set_header Connection "upgrade";
  3. proxy_connect_timeout 4s; #为了防止socket链接很快就断掉需要按照各自的需求配置如下三个参数
  4. proxy_read_timeout 7200s;
  5. proxy_send_timeout 12s;

设置请求最大值

  1. location里面设置:
  2. client_max_body_size 1000M;

参考文档

nginx中文文档:http://www.nginx.cn/doc/