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配置并兼容HTTP实现代码解析
Mar 31 Servers
Nginx+SpringBoot实现负载均衡的示例
Mar 31 Servers
提升Nginx性能的一些建议
Mar 31 Servers
小程序后台PHP版本部署运行 LNMP+WNMP
Apr 01 Servers
Tomcat弱口令复现及利用
May 06 Servers
排查Tomcat进程假死的问题
May 06 Servers
Nginx 配置 HTTPS的详细过程
May 30 Servers
解决Vmware虚拟机安装centos8报错“Section %Packages Does Not End With %End. Pane Is Dead”
Jun 01 Servers
Nginx安装配置详解
Jun 25 Servers
nginx静态资源的服务器配置方法
Jul 07 Servers
详解apache编译安装httpd-2.4.54及三种风格的init程序特点和区别
Jul 15 Servers
Linux在两个服务器直接传文件的操作方法
Aug 05 Servers
Nginx设置HTTPS的方法步骤 443证书配置方法
nginx共享内存的机制详解
Nginx的基本概念和原理
解决xampp安装后Apache无法启动
Nginx图片服务器配置之后图片访问404的问题解决
Shell脚本一键安装Nginx服务自定义Nginx版本
Nginx实现会话保持的两种方式
Mar 18 #Servers
You might like
PHP新手上路(六)
2006/10/09 PHP
PHP中文件上传的一个问题
2010/09/04 PHP
PHP中unset,array_splice删除数组中元素的区别
2014/07/28 PHP
Laravel中使用FormRequest进行表单验证方法及问题汇总
2016/06/19 PHP
CI框架实现优化文件上传及多文件上传的方法
2017/01/04 PHP
PHP策略模式定义与用法示例
2017/07/27 PHP
dojo 之基础篇
2007/03/24 Javascript
兼容IE/Firefox/Opera/Safari的检测页面装载完毕的脚本Ext.onReady的实现
2009/07/14 Javascript
safari,opera嵌入iframe页面cookie读取问题解决方法
2010/06/23 Javascript
javascript表单验证 - Parsley.js使用和配置
2013/01/25 Javascript
js中的eventType事件及其浏览器支持性介绍
2013/11/29 Javascript
JavaScript 学习笔记之数据类型
2015/01/14 Javascript
原生js实现图片轮播特效
2015/12/18 Javascript
JavaScript下的时间格式处理函数Date.prototype.format
2016/01/27 Javascript
ajax分页效果(bootstrap模态框)
2017/01/23 Javascript
微信小程序带动画弹窗组件使用方法详解
2018/11/27 Javascript
CryptoJS中AES实现前后端通用加解密技术
2018/12/18 Javascript
微信小程序配置服务器提示验证token失败的解决方法
2019/04/03 Javascript
layui 弹出层回调获取弹出层数据的例子
2019/09/02 Javascript
layer扩展打开/关闭动画的方法
2019/09/23 Javascript
Vue+scss白天和夜间模式切换功能的实现方法
2021/01/05 Vue.js
十个Python程序员易犯的错误
2015/12/15 Python
pyenv命令管理多个Python版本
2017/03/26 Python
PyQt5+requests实现车票查询工具
2019/01/21 Python
Python分割训练集和测试集的方法示例
2019/09/19 Python
pytorch:model.train和model.eval用法及区别详解
2020/02/20 Python
今天学到的CSS最新技术(与图片背景相关)
2012/12/24 HTML / CSS
HEMA英国:荷兰原创设计
2018/08/28 全球购物
法国二手MacBook销售网站:Okamac
2019/03/18 全球购物
个性化皮包、小袋、生活配件:Mon Purse
2019/03/26 全球购物
目标责任书范文
2014/04/14 职场文书
教师民族团结演讲稿
2014/08/27 职场文书
授权委托书协议书
2014/10/16 职场文书
设备技术员岗位职责
2015/04/11 职场文书
2016新教师岗前培训心得体会
2016/01/08 职场文书
Python安装使用Scrapy框架
2022/04/12 Python