nginx常用配置conf的示例代码详解


Posted in Servers onMarch 21, 2022

nginx常用配置conf

代理静态文件

# 静态文件
server { 
    # 压缩问价你配置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain text/css  application/javascript application/json image/jpeg image/png image/gif;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    listen       8088;
    server_name web_resources;
    root   /data/nginx/static; 
    # 开启页面文件显示
    autoindex on; 
    location / {  
    # add_header Cache-Control no-store;
    add_header Cache-Control "public,max-age=2592000";  
    add_header 'Access-Control-Allow-Origin' '*';
    }
}

配置VUE项目

server {
      listen 8071 ;
      listen [::]:8071 ;
      server_name zrzyweb; # 这里是网站的域名
      location / {
      add_header 'Access-Control-Allow-Origin' $http_origin;
      add_header 'Access-Control-Allow-Credentials' 'true';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
      if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
      }
      root E:/data/vue/zrzy; # /vue/dist/ 打包后的dist目录
      try_files $uri $uri/ @router; # 指向下面的 @router否则会出现 404
      index index.html index.htm;
      }
        # 对应上面的 @router,主要Vue请求并不是真实路径,无法找到文件,需要重定向到 index.html 中,然后交给路由处理
      location @router {
      rewrite ^.*$ /index.html last;
      }
}

配置接口代理

可以配置多个代理服务

# 接口服务 
server {                                        
        listen       8090;
        server_name  njzy.natapp1.cc;
        charset utf-8;
        location /project/ {  
        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_pass http://192.168.1.106:8080/;
        }

        location /one/ {  
        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_pass http://192.168.1.243:9000/;
        }
}

完整nginx.conf配置文件

{
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #location ~ /\.ht {
        #    deny  all;
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
# vue    
server {
        listen 8071 ;
        listen [::]:8071 ;
         server_name zrzyweb; # 这里是网站的域名
   # root /var/www/ec; # /vue/dist/ 打包后的dist目录
      add_header 'Access-Control-Allow-Origin' $http_origin;
      add_header 'Access-Control-Allow-Credentials' 'true';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
      add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
      if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
      }
                root E:/data/vue/zrzy; # /vue/dist/ 打包后的dist目录
                try_files $uri $uri/ @router; # 指向下面的 @router否则会出现 404
                index index.html index.htm;
        # 对应上面的 @router,主要Vue请求并不是真实路径,无法找到文件,需要重定向到 index.html 中,然后交给路由处理
        location @router {
            rewrite ^.*$ /index.html last;
         }
# 静态文件
server {  #web_resources
       gzip on;
       gzip_min_length 1k;
       gzip_buffers 4 16k;
       gzip_http_version 1.1;
       gzip_comp_level 6;
       gzip_types text/plain text/css  application/javascript application/json image/jpeg image/png image/gif;
       gzip_disable "MSIE [1-6]\.";
       gzip_vary on;
                                                  
        listen       8088;
        server_name web_resources;
        root   /data/nginx/static;  
        autoindex on; 
        location / {  
        # add_header Cache-Control no-store;
        add_header Cache-Control "public,max-age=2592000";  
	      add_header 'Access-Control-Allow-Origin' '*';
    } 
 
# 接口服务 
server {                                        
        listen       8090;
        server_name  njzy.natapp1.cc;
        charset utf-8;
        location /project/ {  
        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_pass http://192.168.1.106:8080/;
        location /one/ {  
        proxy_pass http://192.168.1.243:9000/;
      
        location /two/ {  
        proxy_pass http://192.168.1.100:9000/;
        location /three/ {  
        proxy_pass http://192.168.1.100:8085/;
    }

到此这篇关于nginx常用配置conf的文章就介绍到这了,更多相关nginx配置conf内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
nginx location优先级的深入讲解
Mar 31 Servers
Linux安装apache服务器的配置过程
Nov 27 Servers
Nginx性能优化之Gzip压缩设置详解(最大程度提高页面打开速度)
Feb 12 Servers
Apache Linkis 中间件架构及快速安装步骤
Mar 16 Servers
Apache Pulsar结合Hudi构建Lakehouse方案分析
Mar 31 Servers
nginx.conf配置文件结构小结
Apr 08 Servers
超越Nginx的Web服务器caddy优雅用法
Jun 21 Servers
Windows server 2012 NTP时间同步的实现
Jun 25 Servers
Nginx安装配置详解
Jun 25 Servers
Win2008系统搭建DHCP服务器
Jun 25 Servers
nginx访问报403错误的几种情况详解
Jul 23 Servers
Nginx如何限制IP访问只允许特定域名访问
Jul 23 Servers
Nginx设置HTTPS的方法步骤 443证书配置方法
nginx共享内存的机制详解
Nginx的基本概念和原理
解决xampp安装后Apache无法启动
Nginx图片服务器配置之后图片访问404的问题解决
Shell脚本一键安装Nginx服务自定义Nginx版本
Nginx实现会话保持的两种方式
Mar 18 #Servers
You might like
自己做矿石收音机
2021/03/02 无线电
php实现下载限制速度示例分享
2014/02/13 PHP
PHP中的Streams详细介绍
2014/11/12 PHP
摘自织梦CMS中的图片处理类
2015/08/08 PHP
php使用file函数、fseek函数读取大文件效率对比分析
2016/11/04 PHP
详解php curl带有csrf-token验证模拟提交方法
2018/04/18 PHP
PHP+ajax实现获取新闻数据简单示例
2018/05/08 PHP
Swoole实现异步投递task任务案例详解
2019/04/02 PHP
Javascript写了一个清除“logo1_.exe”的杀毒工具(可扫描目录)
2007/02/09 Javascript
prototype 学习笔记整理
2009/07/17 Javascript
改写一个简单的菜单 弹性大小
2010/12/02 Javascript
jsp+javascript打造级连菜单的实例代码
2013/06/14 Javascript
原生javascript实现拖动元素示例代码
2014/09/01 Javascript
微信小程序 免费SSL证书https、TLS版本问题的解决办法
2016/12/14 Javascript
Bootstrap多级菜单的实现代码
2017/05/23 Javascript
Angular 2 ngForm中的ngModel、[ngModel]和[(ngModel)]的写法
2017/06/29 Javascript
js 提取某()特殊字符串长度的实例
2017/12/06 Javascript
vue-better-scroll 的使用实例代码详解
2018/12/03 Javascript
Vue 进阶之路(三)
2019/04/18 Javascript
微信小程序云开发如何使用云函数生成二维码
2019/05/18 Javascript
Vue 实现输入框新增搜索历史记录功能
2019/10/15 Javascript
详解vue父子组件状态同步的最佳方式
2020/09/10 Javascript
Centos5.x下升级python到python2.7版本教程
2015/02/14 Python
Python数据分析之双色球基于线性回归算法预测下期中奖结果示例
2018/02/08 Python
python中协程实现TCP连接的实例分析
2018/10/14 Python
详解python中eval函数的作用
2019/10/22 Python
python3实现绘制二维点图
2019/12/04 Python
Python做图像处理及视频音频文件分离和合成功能
2020/11/24 Python
社区反邪教工作方案
2014/06/16 职场文书
关于读书的演讲稿400字
2014/08/27 职场文书
2014年人事科工作总结
2014/11/19 职场文书
导游欢送词
2015/01/31 职场文书
转学证明范本
2015/06/19 职场文书
Nginx工作原理和优化总结。
2021/04/02 Servers
Python爬虫入门案例之爬取去哪儿旅游景点攻略以及可视化分析
2021/10/16 Python
Redis基本数据类型Zset有序集合常用操作
2022/06/01 Redis