Loading

nginx配置初步

nginx配置初步

1,切换至nginx目录,找到配置文件目录

cd /etc/nginx/conf.d

2,拷贝一份conf文件

sudo cp default.conf head.conf

3,进行conf文件的配置

server{
        listen 80;
        server_name head.cmbc.com.cn;
        proxy_intercept_errors on;
        error_page 404 403 401 /error/40x.html;
        location / {
                proxy_pass http://127.0.0.1:9100;
                include proxy.conf;
        }

}

4,进行nginx配置测试 

sudo nginx -t

5,进行nginx重新启动

sudo nginx -s reload

6,查看上一级目录中的配置

cd ..

cat nginx.conf

user www;
worker_processes 2;

#pid  logs/nginx.pid;
events{
        worker_connections 1024;
}

http{
        include mine.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_forworded_for" "$request_time"';

        access_log /var/log/nginx/access.log main;

        sendfile     on;
        keepalive_timeout 65;

        gzip on;
        gzip_static on;
        gzip_vary on;
        gzip_http_version 1.0;
        gzip_proxied any;
        gzip_disable "MSIE [1-6]\.";
        gzip_comp_level 5;
        gzip_min_length 1000;
        gzip_buffers  4 16k;
        gzip_types text/plain application/javascript text/javascript application/x-javascript text/css text/xml;

        include conf.d/*.conf;  #这句就说明包含了conf.d下面所有的conf文件

}

7, 动静分离

server{
    listen 80;
    server_name s100;
    access_log off;

    location ~* \.(png|html|js|css)$ {
        proxy_pass http://statics;
        #所有以.png .html .js .css结尾的url进入此路径
    }
    location / {
        proxy_pass http://tomcats;
        #其它url进入此路径
    }
}

    server {
        listen       80;
        server_name  pdb.tinyspace.cn;
        index index.html;
        add_header via $upstream_addr;

        location / {
            proxy_pass    http://localhost:8082;
            include proxy.conf;
        }
        location /admin {
            proxy_pass   http://localhost:8082;
            include proxy.conf;
        }
        location ~* \.(html|jpg|jpeg|png|css|scss|sass|js|tpl|ico)$ {
            root /app/src/main/webapp/;
        }
    }

# proxy.conf:
proxy_redirect     off;
proxy_set_header Host $host;
client_max_body_size    15m;

这个要看:https://www.cnblogs.com/IPYQ/p/7889399.html

本地运行项目,有context情况下的配置,使用alias或者rewrite方式

    server {
        listen 9090;
        #location ^~ /context/ajax/ {
        #   proxy_pass      http://127.0.0.1:8080;
        #   proxy_redirect     off;
        #   proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        #}
        #location ^~ /trade/ {
        #  expires 0;
        #  alias /workspace/app/src/main/webapp/;
        #}
        location ^~ /context/ajax/ {
            proxy_pass    http://127.0.0.1:8080;
            include proxy.conf;
        }
        location ^~ /webapp/ {
            root /workspace/app/src/main/;   
        }
        location ~* ^/context/(aaa|js|css|images|page)/ {
            rewrite /context/(.*) /webapp/$1 last;
            #alias /workspace/app/src/main/webapp/;
        }

代理tcp协议连接mysql等

https://www.cnblogs.com/heruiguo/p/8962243.html#_label2_0

https://www.cnblogs.com/guogangj/p/5207104.html

events {
}
http {
}
stream {

    upstream cloudsocket {
       hash $remote_addr consistent;
      # $binary_remote_addr;
       server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
       listen 3306;#数据库服务器监听端口
       proxy_connect_timeout 10s;
       proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
       proxy_pass cloudsocket;
    }
}

客户端请求之后,出现resource interpreted as stylesheet but transferred with mime type text/plain <URL> 问题

注意,在http模块中保留

include            mime.types;
default_type    application/octet-stream;
使用jquery的append方式写入css文件,注意在chrome调试的时候选中Disable cache;

 

posted @ 2018-04-24 08:57  stono  阅读(180)  评论(0编辑  收藏  举报