参考资料:
Nginx (engine x) 是一个轻量级高性能的HTTP和反向代理服务器,同时也是一个通用 代理服务器 (TCP/UDP/IMAP/POP3/SMTP),最初由俄罗斯人Igor Sysoev编写。
下载
官网下载地址:https://nginx.org/en/download.html
如下载 1.18.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| mkdir nginx
wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.10.0.tar.gz -C /usr/local/ mv nginx-1.10.0 nginx cd nginx
yum -y install gcc-c++ yum -y install pcre pcre-devel yum -y install zlib zlib-devel yum -y install openssl openssl-devel
./configure make && make install
whereis nginx nginx -v
|
基本命令
1 2 3 4 5
| nginx -t 检查配置文件是否有语法错误 nginx -c /xxx/nginx.conf 指定配置文件启动 nginx -s reload 热加载,重新加载配置文件 nginx -s stop 快速关闭 nginx -s quit 等待工作进程处理完成后关闭
|
默认配置
Nginx 安装目录下, 复制一份nginx.conf
成nginx.conf.default
作为配置文件备份,然后修改nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| worker_processes 1; events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
log_format access '$remote_addr - $remote_user [$time_local] $host "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$clientip"'; access_log /srv/log/nginx/access.log access; gzip on; sendfile on;
keepalive_timeout 60;
server { listen 8080; server_name localhost;
charset utf-8; access_log logs/localhost.access.log access;
location / { root www; index index.html index.htm; } }
include servers/*; }
|
搭建站点
在其他配置文件servers
目录下,添加新建站点配置文件 xx.conf。
电脑 hosts 文件添加 127.0.0.1 xx_domian
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { listen 8080; server_name xx_domian;
charset utf-8; access_log logs/xx_domian.access.log access;
location / { root www; index index.html index.htm; } }
|
执行命令 nginx -s reload,成功后浏览器访问 xx_domian 就能看到你的页面
根据文件类型设置过期时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| location ~.*\.css$ { expires 1d; break; } location ~.*\.js$ { expires 1d; break; }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { access_log off; expires 15d; break; }
|
禁止文件缓存
开发环境经常改动代码,由于浏览器缓存需要强制刷新才能看到效果。这是我们可以禁止浏览器缓存提高效率
1 2 3
| location ~* \.(js|css|png|jpg|gif)$ { add_header Cache-Control no-store; }
|
防盗链
可以防止文件被其他网站调用
1 2 3 4 5 6 7
| location ~* \.(gif|jpg|png)$ { valid_referers none blocked 192.168.0.1; if ($invalid_referer) { rewrite ^/ http://$host/logo.png; } }
|
静态文件压缩
1 2 3 4 5 6 7 8 9 10 11 12
| server { gzip on; gzip_http_version 1.1; gzip_comp_level 4; gzip_min_length 1000; gzip_types text/plain application/javascript text/css; }
|
执行命令 nginx -s reload,成功后浏览器访问
指定定错误页面
1 2 3 4 5
| error_page 500 502 503 504 /50x.html; location = /50x.html { root /source/error_page; }
|
执行命令 nginx -s reload,成功后浏览器访问
跨域问题
跨域的定义
同源策略限制了从同一个源加载的文档或脚本如何与来自另一个源的资源进行交互。这是一个用于隔离潜在恶意文件的重要安全机制。通常不允许不同源间的读操作。
同源的定义
如果两个页面的协议,端口(如果有指定)和域名都相同,则两个页面具有相同的源。
nginx解决跨域的原理
例如:
- 前端server域名为:
http://xx_domain
- 后端server域名为:
https://github.com
现在http://xx_domain
对https://github.com
发起请求一定会出现跨域。
不过只需要启动一个nginx服务器,将server_name
设置为xx_domain
,然后设置相应的location以拦截前端需要跨域的请求,最后将请求代理回github.com
。如下面的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server { listen 8080; server_name xx_domain
location / { proxy_pass https://github.com; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
|
这样可以完美绕过浏览器的同源策略:github.com
访问nginx
的github.com
属于同源访问,而nginx
对服务端转发的请求不会触发浏览器的同源策略。
Nginx配置参数中文详细说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| user www www;
worker_processes 8;
error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;
events { use epoll; worker_connections 65535; }
http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 64k; client_max_body_size 8m;
autoindex on; autoindex_exact_size on; autoindex_localtime on;
sendfile on; tcp_nopush on; tcp_nodelay on;
keepalive_timeout 120;
fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k;
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on;
server { listen 80; server_name ably.com; rewrite ^(.*) https://$server_name$1 permanent; }
server { listen 443 ssl; server_name ably.com;
ssl_certificate C:\WebServer\Certs\certificate.crt; ssl_certificate_key C:\WebServer\Certs\private.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on;
index index.html index.htm index.php; root /data/www/; location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
location /oauth/ { proxy_pass https://localhost:13580/oauth/; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 10d; }
location ~ .*\.(js|css)?$ { expires 1h; }
log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /var/log/nginx/access.log access;
location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } } }
|
Nginx多台服务器实现负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| 1.Nginx负载均衡服务器: IP:192.168.0.4(Nginx-Server) 2.Web服务器列表: Web1:192.168.0.5(Nginx-Node1/Nginx-Web1) ;Web2:192.168.0.7(Nginx-Node2/Nginx-Web2) 3.实现目的:用户访问Nginx-Server(“http://mongo.demo.com:8888”)时,通过Nginx负载均衡到Web1和Web2服务器 Nginx负载均衡服务器的nginx.conf配置注释如下:
events { use epoll; worker_connections 65535; } http { upstream webhost { server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; } upstream webhost { server 192.168.0.5:6666 weight=2; server 192.168.0.7:6666 weight=3; } upstream webhost { ip_hash; server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; } upstream webhost { server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; hash $request_uri; } server { listen 80; server_name mongo.demo.com; location / { proxy_pass http://webhost; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } }
负载均衡操作演示如下: 操作对象:192.168.0.4(Nginx-Server)
$ mkdir -p /opt/confs $ vim /opt/confs/nginx.conf
events { use epoll; worker_connections 65535; }
http { upstream webhost { ip_hash; server 192.168.0.5:6666 ; server 192.168.0.7:6666 ; }
server { listen 80; server_name mongo.demo.com; location / { proxy_pass http://webhost; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } } }
docker run -d -p 8888:80 --name nginx-server -v /opt/confs/nginx.conf:/etc/nginx/nginx.conf --restart always nginx 操作对象:192.168.0.5(Nginx-Node1/Nginx-Web1)
$ mkdir -p /opt/html $ vim /opt/html/index.html
<div> <h1> The host is 192.168.0.5(Docker02) - Node 1! </h1> </div>
$ docker run -d -p 6666:80 --name nginx-node1 -v /opt/html:/usr/share/nginx/html --restart always nginx 操作对象:192.168.0.7(Nginx-Node2/Nginx-Web2)
$ mkdir -p /opt/html $ vim /opt/html/index.html
<div> <h1> The host is 192.168.0.7(Docker03) - Node 2! </h1> </div>
$ docker run -d -p 6666:80 --name nginx-node2 -v $(pwd)/html:/usr/share/nginx/html --restart always nginx
|