CentOS 7 安装 Nginx 反向代理 node

安装 nginx

yum install epel-release

yum install nginx

配置 nginx

sudo vim /etc/nginx/nginx.conf, 改成下面配置:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/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;
    keepalive_timeout   65;

    include /etc/nginx/conf.d/*.conf;
}

在/etc/nginx/conf.d下面新建自己 node server 的配置文件,我的 node server 监听 3000 端口,想用 xxx.yyy.com 域名来访问。
sudo vim /etc/nginx/conf.d/www.yyy.com.conf, 写入如下内容:

server {
        listen 80;
        server_name node.appnongye.com;
        location / {
                proxy_pass http://127.0.0.1:3000/;
                proxy_redirect     off;
                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;
        }
}

nginx 配置语法检查

sudo nginx -t

nginx 重启

sudo systemctl restart nginx

实际配置:

[root@la-ss-portal ~]# cat /etc/nginx/conf.d/proxy.conf
server {
listen 80;
server_name 148.153.0.154;
location / {
proxy_pass http://www.abc360.com/;
proxy_redirect off;
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@la-ss-portal ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
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;
keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
}

error_page 404 /404.html;
location = /40x.html {
}

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

# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }

}

测试:

[root@la-ss-portal ~]# curl 148.153.0.154
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="ie6 oldie no-js"> <![endif]-->
<!--[if IE 7]> <html class="ie7 oldie no-js"> <![endif]-->
<!--[if IE 8]> <html class="ie8 oldie no-js"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
<head>
<meta charset="UTF-8">
<title>abc360-在线英语学习_外教一对一在线英语口语培训</title>
<script>
var PUB_URI="/Public/Web/New/",
BASE_CONTROLLER="/Web/New/",
APP="",
MEMBER_CENTER="/MemberCenter/",
PWD_REGX=/^[w-_]{6,20}$/,
PWD_ERRMSG="密码应为6-20位数字或英文字母",
PWD_HINT="请设置6-20位数字、英文字母组合密码",
STD_ID="0",
LOGGED_IN=0;
</script> <meta name="viewport" content="width=device-width, initial-sca

原文地址:https://www.cnblogs.com/weifeng1463/p/7064368.html