Nginx安裝和反向代理配置javascript
Nginx安裝須要一些準備工做。 安裝gcc等css
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
還須要安裝pcre,PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 Perl兼容的正則表達式庫。
html
yum -y install pcre
下載Nginx源碼包,這裏選擇是1.7.8版本。而且解壓縮,而且編譯
java
wget http://nginx.org/download/nginx-1.7.8.tar.gz tar zxcf nginx-1.7.8.tar.gz ./configure --prefix=/usr/local/nginx --with-pcre //--prefix=表示安裝的路徑 make && make install
編譯成功後,進入Nginx目錄查看下版本,驗證是否安裝完成。node
[root@ACtest ~]# cd /usr/local/nginx/sbin/ [root@ACtest sbin]#/usr/local/nginx/sbin/nginx //啓動Nginx [root@ACtest sbin]# ./nginx -v //驗證下Nginx版本 nginx version: nginx/1.7.8 [root@chumjtest sbin]# ps aux|grep nginx //查看下Nginx啓動進程 root 4684 0.0 0.0 103256 840 pts/1 S+ 15:14 0:00 grep nginx root 32670 0.0 0.0 24304 700 ? Ss Dec23 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 32671 0.0 0.0 26804 3824 ? S Dec23 0:00 nginx: worker process nobody 32672 0.0 0.0 26748 3396 ? S Dec23 0:16 nginx: worker process
安裝完成後對Nginx進行配置nginx
[root@ACtest ~]#cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak //將主配置文件進行備份 [root@ACtest ~]#vim /usr/local/nginx/conf/nginx.conf //這裏將原來的配置文件清空,替換新的配置文件。 user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; }
將重啓下Nginx,將nginx進程Kill掉便可
c++
配置反向代理web
經過nginx訪問不一樣的web服務器的端口正則表達式
如:localhost:9080 = 10.1.1.78:9080vim
在配置文件加入
server { listen 9080; //代理的端口 server_name localhost; //本機的信息 location / { proxy_pass http://10.1.1.78:9080/; //表示代理的地址 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # access_log /home/logs/bb_access.log combined; }
配置完後,重啓下Nginx。以後測試下。
[root@ACtest sbin]# curl -I http://localhost:9080 HTTP/1.1 200 OK Server: nginx/1.7.8 Date: Mon, 26 Dec 2016 07:52:37 GMT Content-Type: text/html Content-Length: 1812 Connection: keep-alive Last-Modified: Tue, 14 Jun 2016 01:53:30 GMT ETag: "714-53533459b292a" Accept-Ranges: bytes
測試成功,表示網站能夠正常訪問。