2025年3月15日 星期六 甲辰(龙)年 月十四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

Nginx:常用场景的配置示例

时间:05-29来源:作者:点击数:35

Nginx:常用场景的配置示例

1、HTTP服务器

  • server {
  • listen 80;
  • server_name localhost;
  • client_max_body_size 1024M;
  • # 默认location
  • location / {
  • root /usr/local/var/www/html;
  • index index.html index.htm;
  • }
  • }

2、静态服务器

  • server {
  • listen 80;
  • server_name localhost;
  • set $doc_root /usr/local/var/www;
  • # 默认location
  • location / {
  • root /usr/local/var/www/html;
  • index index.html index.htm;
  • }
  • location ^~ /images/ {
  • root $doc_root;
  • }
  • location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
  • root $doc_root/img;
  • }
  • }

3、反向代理

  • server {
  • listen 80;
  • server_name localhost;
  • location / {
  • proxy_pass http://localhost:8081;
  • # 设置用户ip地址
  • proxy_set_header Host $host;
  • proxy_set_header X-Real-IP $remote_addr;
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  • # 当请求服务器出错去寻找其他服务器
  • proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
  • }
  • }

4、负载均衡

  • upstream web_servers {
  • server localhost:8081;
  • server localhost:8082;
  • }
  • server {
  • listen 80;
  • server_name localhost;
  • #access_log logs/host.access.log main;
  • location / {
  • proxy_pass http://web_servers;
  • # 必须指定Header Host
  • proxy_set_header Host $host:$server_port;
  • }
  • }

5、动静分离

  • upstream web_servers {
  • server localhost:8081;
  • server localhost:8082;
  • }
  • server {
  • listen 80;
  • server_name localhost;
  • set $doc_root /usr/local/var/www;
  • location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
  • root $doc_root/img;
  • }
  • location / {
  • proxy_pass http://web_servers;
  • # 必须指定Header Host
  • proxy_set_header Host $host:$server_port;
  • }
  • error_page 500 502 503 504 /50x.html;
  • location = /50x.html {
  • root $doc_root;
  • }
  • }

6、其他

return指令

  • location /permanently/moved/url {
  • return 301 http://www.example.com/moved/here;
  • }

rewrite指令

  • location /users/ {
  • rewrite ^/users/(.*)$ /show?user=$1 break;
  • }

error_page指令

  • error_page 404 /404.html;

日志

  • log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  • '$status $body_bytes_sent "$http_referer" '
  • '"$http_user_agent" "$http_x_forwarded_for"';
  • access_log /usr/local/etc/nginx/logs/host.access.log main;
  • gzip on;

deny 指令

  • # 禁止访问某个目录
  • location ~* \.(txt|doc)${
  • root $doc_root;
  • deny all;
  • }

内置变量

  • $args:这个变量等于请求行中的参数,同$query_string
  • $content_length:请求头中的Content-length字段。
  • $content_type:请求头中的Content-Type字段。
  • $document_root:当前请求在root指令中指定的值。
  • $host:请求主机头字段,否则为服务器名称。
  • $http_user_agent:客户端agent信息
  • $http_cookie:客户端cookie信息
  • $limit_rate:这个变量可以限制连接速率。
  • $request_method:客户端请求的动作,通常为GET或POST。
  • $remote_addr:客户端的IP地址。
  • $remote_port:客户端的端口。
  • $remote_user:已经经过Auth Basic Module验证的用户名。
  • $request_filename:当前请求的文件路径,由root或alias指令与URI请求生成。
  • $scheme:HTTP方法(如http,https)。
  • $server_protocol:请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  • $server_addr:服务器地址,在完成一次系统调用后可以确定这个值。
  • $server_name:服务器名称。
  • $server_port:请求到达服务器的端口号。
  • $request_uri:包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
  • $uri:不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
  • $document_uri:与$uri相同

常用完整配置

  • # 处理@记录
  • server {
  • listen 80;
  • server_name example.com;
  • listen 443 ssl;
  • ssl_certificate /usr/local/nginx/conf/ssl/www.example.com.pem;
  • ssl_certificate_key /usr/local/nginx/conf/ssl/www.example.com.key;
  • return 301 https://www.example.com$request_uri;
  • }
  • # 处理www记录
  • server {
  • listen 80;
  • listen 443 ssl http2;
  • server_name www.example.com;
  • ssl_certificate /usr/local/nginx/conf/ssl/www.example.com.pem;
  • ssl_certificate_key /usr/local/nginx/conf/ssl/www.example.com.key;
  • add_header Strict-Transport-Security max-age=15768000;
  • # 自动跳转到https
  • if ($ssl_protocol = "") { return 302 https://$host$request_uri; }
  • if ($time_iso8601 ~ '(\d{4}-\d{2}-\d{2})') {
  • set $time $1;
  • }
  • access_log /data/wwwlogs/nginx_log/www.example.com_${time}.log main;
  • #error_page 404 /404.html;
  • #error_page 502 /502.html;
  • root /data/wwwroot/www.example.com/public;
  • # www代码
  • location / {
  • index index.html;
  • }
  • # 后台 vue实现
  • location ^~/admin {
  • alias /data/wwwroot/www.example.com/admin;
  • try_files $uri $uri/ /admin/index.html;
  • }
  • # 前台静态文件
  • location ^~ /static/ {
  • alias /data/wwwroot/www.example.com/public/static/;
  • }
  • location / {
  • proxy_pass http://127.0.0.1:5001;
  • proxy_set_header Host $host;
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  • }
  • location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico|apk)$ {
  • expires 30d;
  • access_log off;
  • }
  • location ~ .*\.(js|css)?$ {
  • expires 7d;
  • access_log off;
  • }
  • location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
  • deny all;
  • }
  • location /.well-known/acme-challenge/ {
  • alias /var/www/challenges/;
  • try_files $uri = 404;
  • }
  • }
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门