Centos 7.3 x64安装nginx 与反向代理的应用

Centos 7.3 x64安装Nginx-1.13.1


Nginx-1.13.1 发布日期2017-05-30


测试完成日期:2017.6.22 by evan.li


一、安装必要所需环境:

# yum install autoconf automake gcc gcc-c++ libtool make pkgconfig zlib-devel

# yum install pcre pcre-devel PHP-devel httpd-devel

# yum install zlib zlib-devel

# yum install openssl openssl-devel


# groupadd Nginx

# useradd -g Nginx Nginx

# mkdir /software

# cd /software

# wget http://Nginx.org/download/Nginx-1.13.1.tar.gz

# tar xzf Nginx-1.13.1.tar.gz

# cd Nginx-1.13.1


安装相关依赖包

[root@ns1 Nginx-1.13.1]# yum install libxml2-devel libxslt-devel gd-devel

[root@ns1 Nginx-1.13.1]# yum install perl perl-devel perl-ExtUtils-Embed libatomic_ops-devel



[root@ns1 Nginx-1.13.1]# ./configure --user=Nginx --group=Nginx --sbin-path=/usr/sbin/Nginx --conf-path=/etc/Nginx/Nginx.conf --error-log-path=/var/log/Nginx/error.log --http-log-path=/var/log/Nginx/access.log --pid-path=/var/run/Nginx.pid --with-select_module --with-poll_module --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-cpp_test_module --with-cpu-opt=cpu --with-pcre --with-pcre-jit --with-zlib-asm=cpu --with-libatomic --with-debug --with-ld-opt="-Wl,-E" --http-client-body-temp-path=/var/tmp/Nginx/client/ --http-proxy-temp-path=/var/tmp/Nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/Nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi --http-scgi-temp-path=/var/tmp/Nginx/scgi


[root@ns1 Nginx-1.13.1]#

....成功结果如下

checking for atomic_ops library ... found

creating objs/Makefile

Configuration summary

+ using system PCRE library

+ using system OpenSSL library

+ using system zlib library

+ using system libatomic_ops library

Nginx path prefix: "/usr/local/Nginx"

Nginx binary file: "/usr/sbin/Nginx"

Nginx modules path: "/usr/local/Nginx/modules"

Nginx configuration prefix: "/etc/Nginx"

Nginx configuration file: "/etc/Nginx/Nginx.conf"

Nginx pid file: "/var/run/Nginx.pid"

Nginx error log file: "/var/log/Nginx/error.log"

Nginx http access log file: "/var/log/Nginx/access.log"

Nginx http client request body temporary files: "/var/tmp/Nginx/client/"

Nginx http proxy temporary files: "/var/tmp/Nginx/proxy/"

Nginx http fastcgi temporary files: "/var/tmp/Nginx/fcgi/"

Nginx http uwsgi temporary files: "/var/tmp/Nginx/uwsgi"

Nginx http scgi temporary files: "/var/tmp/Nginx/scgi"

./configure: warning: the "--with-ipv6" option is deprecated


[root@ns1 Nginx-1.13.1]# make

[root@ns1 Nginx-1.13.1]# make install


[root@ns1 Nginx-1.13.1]# Nginx -V

Nginx version: Nginx/1.13.1


将启动脚本(见附件)上传到/etc/init.d/ 目录下面

# chmod +x /etc/init.d/Nginx

# chkconfig --add Nginx

# chkconfig Nginx on

# chkconfig --list Nginx

# mkdir -p /var/tmp/Nginx/client


编辑配置文件,红色字体为修改添加后的项目

# vi /etc/Nginx/Nginx.conf


user root;

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 /etc/Nginx/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 /var/log/Nginx/access.log main;


sendfile on;

tcp_nopush on;

tcp_nodelay on;

types_hash_max_size 2048;

client_max_body_size 20M;

keepalive_timeout 65;


gzip on;

gzip_disable "msie6";

gzip_comp_level 6;

gzip_min_length 1100;

gzip_buffers 16 8k;

gzip_proxied any;

gzip_types text/plain text/css text/js text/xml text/javascript application/javascript application/x-javascript application/json application/xml application/RSS+xml image/svg+xml;


server {

listen 80;

server_name 110.110.220.54;

charset utf-8;


location / {

root /var/www/html/dongying;

index index.html index.htm index.jsp login.jsp;

}


#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

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

#}

}



# 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

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


#}

}



检测配置文件是否有语法错误

[root@host-cn tmp]# Nginx -t -c /etc/Nginx/Nginx.conf

Nginx: the configuration file /etc/Nginx/Nginx.conf Syntax is ok

Nginx: configuration file /etc/Nginx/Nginx.conf test is successful


加入防火墙允许

[root@ns1 Nginx]# firewall-cmd --add-service=http --permanent

[root@ns1 Nginx]# firewall-cmd --reload

[root@ns1 Nginx]# systemctl restart firewalld


启动Nginx服务

[root@ns1 Nginx]#service Nginx start


用户就可以使用IP 110.110.220.54直接访问网站了。


-----------------------------------------------------------------------------------

反向代理的应用

公司同事,用tomcat发布了2个项目,可以用IP访问他的项目

A项目地址 http://110.110.220.54:8080/dy/jsp/index.jsp

B项目地址 http://110.110.220.54:8080/dy/position/login.jsp

现在使用Nginx,用公司域名www.dongying.com来发布他的项目,方法如下:

绿色部分为项目A,设定正常后用www.dongying.com来访问。

橙色部分为项目B,设定正常后用www.dongying.com:8080/dy/position/login.jsp来访问。


listen 80;

server_name www.dongying.com;

charset utf-8;

location / {

proxy_pass http://www.dongying.com:8080/dgmy/jsp/;

index index.html index.htm index.jsp login.jsp;

}


location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$

{

access_log off;

expires 1d;

root /opt/tomcat/webapps/dy/;

break;

}

location /dy/position/ {

proxy_pass http://www.dongying.com:8080/dy/position/login.jsp;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 100m;

root html;

index index.html index.htm index.jsp login.jsp;

}



测试完成日期:2017.6.23 by evan.li

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native