静态页面配置:

server {
    listen 80;
    server_name share.zjc123.cn;

    gzip on;
    gzip_vary on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

    location / {
        root /home/project/home_page_zjc;
        index index.html index.htm;
    }
}

Java 服务配置

# proxy_pass为服务地址
server {
    listen 80;
    server_name share.tool.zjc123.cn;

    location / {
        proxy_pass http://localhost:8850;
        proxy_set_header Connection "";
        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_buffers 16 8k;
        proxy_http_version 1.1;
    }

}

SSL服务配置

这里以Java服务为例,静态资源亦是如此。

注意:Nginx 1.8之后配置规则已经更改

Nginx 1.8版本之前的配置
# https
server {
    listen 443;
    server_name zjc123.cn www.zjc123.cn;

    ssl on;
    ssl_certificate /etc/nginx/ssl-crt/1_zjc123.cn_bundle.crt;
    ssl_certificate_key /etc/nginx/ssl-crt/2_zjc123.cn.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://localhost:8850;
        proxy_set_header Connection "";
        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_buffers 16 8k;
        proxy_http_version 1.1;
    }
}

# http
server {
    listen 80;
    server_name zjc123.cn www.zjc123.cn;
    location / {
        proxy_pass http://localhost:8850;
        proxy_set_header Connection "";
        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_buffers 16 8k;
        proxy_http_version 1.1;
    }
}

# 重定向https
server {
    listen 80;
    server_name zjc123.cn www.zjc123.cn;
    # 重定向为https协议(也可以选择不重定向到https)
    rewrite ^(.*)$ https://$host$1 permanent;
}

server {
    listen 80;
    server_name ~^(.*)\.a8tiyu\.com$;
    set $servername $1;
    rewrite ^ $scheme://$servername.a8sport.com$request_uri permanent;
}
Nginx 1.8版本之后的配置
# https
server {
    listen 443 ssl;
    server_name zjc123.cn www.zjc123.cn;

    ssl_certificate /var/www/html/ssl/zjc123.cn/1_zjc123.cn_bundle.crt;
    ssl_certificate_key /var/www/html/ssl/zjc123.cn/2_zjc123.cn.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    location / {
        proxy_pass http://localhost:8850;
        proxy_set_header Connection "";
        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_buffers 16 8k;
        proxy_http_version 1.1;
    }
}

# 重定向https
server {
    listen 80;
    server_name zjc123.cn www.zjc123.cn;
    # 重定向为https协议(也可以选择不重定向到https)
    rewrite ^(.*)$ https://$host$1 permanent;
}

PHP服务配置

server {
    listen 1060;
    server_name	localhost;
    charset utf-8;
    index index.php index.html index.htm;
    # 项目主目录
    root /Users/zhengshangjin/WorkSpace/Idea-A8/A8Cms/app;

    location / {
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=$1 last;
            break;
        }
    }

    location = /50x.html {
        root html;
    }

    location ~ \.php {
        # wwww.conf监听端口
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;

        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;
        #include fastcgi.conf;

        fastcgi_buffers 32 1024k;
        fastcgi_buffer_size 2048k;
        #fastcgi_busy_buffers_size 2048k;
        #fastcgi_temp_file_write_size 2048k;

        # 连接、请求、读取各超时时间
        fastcgi_connect_timeout 75;
        fastcgi_send_timeout 600;
        fastcgi_read_timeout 600;

        # 控制满足该路由规则的请求报文大小
        client_max_body_size 2048m;
        client_body_temp_path /tmp/nginx;
        client_body_buffer_size 20480k;
    }
}
2020-08-31更新配置
server {
    listen 1060;
    server_name	localhost;
    charset utf-8;
    index index.php index.html index.htm;
    root /Users/zhengshangjin/Documents/workspace/Idea-A8/A8Cms/app;

    location / {
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=$1 last;
            break;
        }
    }

    location = /50x.html {
        root html;
    }

    location ~ \.php {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;

        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;
        #include fastcgi.conf;

        fastcgi_buffers 32 1024k;
        fastcgi_buffer_size 2048k;
        #fastcgi_busy_buffers_size 2048k;
        #fastcgi_temp_file_write_size 2048k;

        fastcgi_connect_timeout 75;
        fastcgi_send_timeout 600;
        fastcgi_read_timeout 600;

        client_max_body_size 2048m;
        client_body_temp_path /tmp/nginx;
        client_body_buffer_size 20480k;
    }
}

vue项目

server {
    listen 2099;
    server_name localhost;

    location / {
        root /home/admin/a8Live-cms-vue/;
        index index.html index.htm;
        # 解决刷新404
        try_files $uri $uri/ /index.html;
    }

    location /prod-api/ {
        proxy_pass http://localhost:8899/;
        proxy_set_header Connection "";
        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_buffers 16 8k;
        proxy_http_version 1.1;
    }
}

规则匹配重定向

  • 保留原始请求路径

  • 带 / 与不带,区别在于是否保留 location 后面匹配的路径

  • 若携带参数,则也会重定向到新的地址上

# 请求
http://auth.a8sport.com/tips-test/callback/wx/login
http://auth.a8sport.com/tips-test/callback/wx/login?code=sdasdad&state=123456

# 重定向到
http://test.creator.tips.a8sport.com/tips-test/callback/wx/login
http://test.creator.tips.a8sport.com/tips-test/callback/wx/login?code=sdasdad&state=123456
server {
    listen 80;
    server_name auth.a8sport.com;
    location /tips-test/ {
        rewrite ^/(.*) http://test.creator.tips.a8sport.com/$1 redirect;
    }
}

规则转发请求

server {
    listen 80;
    server_name auth.a8sport.com;
    location /tips-test/ {
        proxy_pass http://test.creator.tips.a8sport.com;
        proxy_set_header Host test.creator.tips.a8sport.com;
    }
}

Q.E.D.