Nginx 的 Vhost 配置

本贴最后更新于 1324 天前,其中的信息可能已经渤澥桑田

vhost 配置文件的作用

作用:vhost 配置文件的作用是为了将多个 server 配置文件的信息,单独存放,不过于集中在 nginx.conf 配置中,这样有用助于故障排除,如配置查看

通过访问服务端 nginx 代理的域名可将流量转发至配置的客户机,客户机将服务端转发过来的流量转发至本机的监听相应端口的服务。

nginx 代理服务端:

#网易云音乐API虚拟主机块配置
server{
    listen 80;
    server_name music163.cjzshilong.cn;
    location / {
            proxy_pass http://127.0.0.1:3001;
               }
}

# HTTPS server

server {
    listen       443 ssl;
    server_name  music163.cjzshilong.cn;

    ssl_certificate      /usr/local/nginx/conf/ssl/1_163.cjzshilong.cn_bundle.crt;
    ssl_certificate_key  /usr/local/nginx/conf/ssl/2_163.cjzshilong.cn.key;

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

    ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers  on;

    location /{
    proxy_set_header Host $host;
    proxy_set_header Connection "Keep-Alive";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffer_size 128k;
    proxy_buffers  32 32k;
    proxy_busy_buffers_size 128k;
    proxy_pass    http://127.0.0.1:3001;
    }

}
  • try_files 将尝试你列出的文件并设置内部文件指向,try_files 方法让 Nginx 尝试访问后面得 $uri 链接,并进根据 @confluence 配置进行内部重定向。当然 try_files 也可以以错误代码赋值,如 try_files /index.php = 404 @confluence,则表示当尝试访问得文件返回 404 时,根据 @confluence 配置项进行重定向。

nginx 配置:

[root@vpc /usr/local/nginx/conf]# cat nginx.conf
user  www www;

error_log /var/log/nginx/error.log error;
pid       /var/run/nginx.pid;
lock_file /var/lock/nginx;

worker_rlimit_nofile 102400;
worker_processes auto;

events {
    worker_connections 10240;
    use epoll;
    multi_accept on;
    accept_mutex off;
}


http {
#core
    include         mime.types;
    default_type    application/octet-stream;
    connection_pool_size        1024;
    client_body_buffer_size     16k;
    client_body_temp_path       /usr/local/nginx/var/tmp/client_body 1 2;
    client_body_timeout         30;
    client_header_buffer_size   4k;
    large_client_header_buffers 4 4k;
    client_header_timeout       30;
    client_max_body_size        32m;
    #keepalive_disable  msie6 safari;
    keepalive_timeout   3;
    tcp_nodelay         on;
    send_timeout 30;
    sendfile    on;
    tcp_nopush  off;
    server_names_hash_max_size      512;
    server_names_hash_bucket_size   128;
    server_tokens off;
    open_file_cache off;
#index
    index   index.php index.html index.htm;
#fastcgi
    fastcgi_connect_timeout     60;
    fastcgi_read_timeout        60;
    fastcgi_send_timeout        60;
    fastcgi_temp_path           /usr/local/nginx/var/tmp/fastcgi 1 2;
    fastcgi_buffer_size         128k;
    fastcgi_buffers             2 256k;
    fastcgi_busy_buffers_size   256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_max_temp_file_size  256k;
    fastcgi_intercept_errors    on;
    fastcgi_index               index.php;
#proxy
    proxy_temp_path             /usr/local/nginx/var/tmp/proxy;
    proxy_buffer_size           4k;
    proxy_buffering             on;
    proxy_buffers               256 4k;
    proxy_busy_buffers_size     8k;
#gzip
    gzip                on;
    gzip_buffers        16 4k;
    gzip_comp_level     6;
    gzip_http_version   1.1;
    gzip_min_length     2000;
    #gzip_types         application/json application/javascript text/javascript image/png text/css text/xml text/plain text/vnd.wap.wml application/x-javascript  application/rss+xml application/xhtml+xml application/json-rpc application/jsonrequest;
    gzip_types          text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png;
    gzip_vary           off;


#realip module
#    set_real_ip_from    127.0.0.1;
#    real_ip_header      X-Real-IP;
    #real_ip_header     X-Forwarded-For;
#log module
    log_format main '$remote_addr - $remote_user [$time_local] $request '
                    '"$status" $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format moss '$remote_addr | $http_x_forwarded_for | $remote_user | [$time_local] |'
                    ' "$request" | $status | $body_bytes_sent |'
                    ' "$http_referer" | "$http_user_agent" | $request_time | $upstream_response_time';
#ClickJacking
#    add_header X-Frame-Options SAMEORIGIN;

#virtualhost
    include vhosts/*.conf;
}
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    914 引用 • 930 回帖
  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    311 引用 • 546 回帖 • 58 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...