Nginx配置

本文档展示了如何配置Nginx以实现多域名支持和HTTPS安全访问。通过在nginx.conf中包含vhost目录下的独立域名配置文件,可以为每个域名设置不同的规则。例如,对于baidu.com及其子域名,配置了强制HTTPS重定向,设置了SSL证书和私钥,并定义了代理到内部API的location规则,以处理lua脚本处理的参数。
摘要由CSDN通过智能技术生成

nginx.conf

#user nginx;
worker_processes auto;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#error_log  logs/error.log  debug;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main escape=none  '$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 {
            root   html;
        }

    
    include  /usr/local/nginx/conf/vhost/*.conf;
}

配置多域名

注意 nginx.conf文件的最下面一行配置进行加载每个独立域名的配置信息

    include  /usr/local/nginx/conf/vhost/*.conf;

app.com

本地配置hosts文件后
vhost目录下创建一个app.conf文件


server {
    listen       80;
    server_name  ~^(www\.)?baidu.com;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
}

server {
    listen       443 ssl;  #监听端口
    server_name  ~^(www\.)?baidu.com;
    charset      UTF-8;
    ssl_certificate      /usr/local/nginx/key/baidu/4919621_www.baidu.com.pem; #证书位置
    ssl_certificate_key  /usr/local/nginx/key/baidu/4919621_www.baidu.com.key; #私钥位置

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  10m;

    ssl_ciphers  HIGH:!aNULL:!MD5; #密码加密方式
    ssl_prefer_server_ciphers  on; # ssl_prefer_server_ciphers  on; #

    access_log  logs/baidu.access.log  main;

    lua_code_cache off;

    location /api {
        # 调用lua脚本处理参数url encode转义操作
        set_by_lua_file $modify_args '/usr/local/nginx/script/lua/encode_args.lua';

        proxy_redirect off;
        proxy_pass http://127.0.0.1:8081$uri?$modify_args;
        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_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
    }

    location / {
        root   html/baidu/https;
        index  index.html index.htm;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值