Centos7安裝 nginx 及简单配置

安裝 nginx

# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安装在目录:/etc/yum.repos.d/nginx.repo)
# yum install nginx

启动并查看状态

# 启动服务
systemctl start nginx
# 查看状态
systemctl status nginx
# 开机自启动
systemctl enable nginx

配置nginx

Http反向代理

编辑配置

# cd /etc/nginx/conf.d
# cp default.conf  mysite.conf 

修改mysite.conf 如下:

server {
    listen  80;
    server_name 192.168.0.18;
    #access_log /var/log/nginx/pro.log;

    location  / {
         # 传递真实的请求头信息
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         # 限制文件大小为1G 
         client_max_body_size    1024m; 
         # 指定代理服务器
         proxy_pass http://127.0.0.1:8000; 
    }
        
    location /static/ {
        autoindex on;
        alias /root/webapps/pro/static/; # 静态文件nginx处理
    }

    error_page 500 502 503 504  /50x.html;
    location = /50x.html {
        root html;
    }
}

Tcp反向代理

注意tcp只能代理端口

stream {
  upstream mysql {
    hash $remote_addr consistent;
    server MySQL_URL:3306 max_fails=3 fail_timeout=30s;
  }

  server {
    listen 8000;
    proxy_connect_timeout 30s;
    proxy_timeout 600s;
    proxy_pass mysql;
  }
}

域名代替子级目录

需求:(隐藏子级目录)通过访问a.hostname:port 访问代替 http://hostname:port/a/

server {
        listen  80;
        server_name a.xxx.com;
        #access_log /var/log/nginx/pro.log;

        location  / {
             # 传递真实的请求头信息
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             # 限制文件大小为10G 
             client_max_body_size    10240m; 
             # 域名重写
             rewrite ^/(.*)$ /a/$1 last;
        }
        
        location ~* ^/a/.*$ {  
            proxy_pass http://localhost:80;  
        }  

        error_page 500 502 503 504  /50x.html;
        location = /50x.html {
            root html;
        }
    }

完整配置参考


#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;
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            # index  index.html index.htm;
            
            rewrite ^/(.*)$ /man/$1 last;
        }
        
        location /man {
            alias   C:/Users/Public/www;
            autoindex on;
        }

        location /api {
            rewrite ^.+api/?(.*)$ /$1 break;
            include uwsgi_params;
            proxy_pass http://172.18.5.175:9999;
        }
    
        #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;
        }

        # 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
        #
        #location ~ \.php$ {
        #    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;
        #}
    }
    

    server {
        listen  80;
        server_name man.xxx.com;
        #access_log /var/log/nginx/pro.log;

        location  / {
             # 传递真实的请求头信息
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             # 限制文件大小为1G 
             client_max_body_size    10240m; 
             
             rewrite ^/(.*)$ /man/$1 last;
        }
        
        location ~* ^/man/.*$ {  
            proxy_pass http://172.18.5.152:80;  
        }  
    
        error_page 500 502 503 504  /50x.html;
        location = /50x.html {
            root html;
        }
    }

    # 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
    #
    #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;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
posted @ 2017-04-03 18:48  刘文江  阅读(10)  评论(0)    收藏  举报  来源