ubantu16 install nginx18

安装必备包

sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install libtool

下载安装

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

./configure --prefix=/usr/local/nginx --with-http_ssl_module
sudo make
sudo make install

nginx.sh脚本

#!/bin/bash
#
# Simple nginx init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     nginx_80
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Nginx data structure server
# Description:          Nginx data structure server. See http://nginx.org/en/download.html
### END INIT INFO

set -e
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
CONF=/opt/app/config/nginx.conf


# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0

d_start() {
        $DAEMON -c $CONF || echo -n " already running"
}

d_stop() {
        $DAEMON -s quit || echo -n " not running"
}

d_reload() {
        $DAEMON -s reload || echo -n " could not reload"
}

case "$1" in
    start)
    echo -n "Starting $DESC: $NAME"
    d_start
    echo "."
    ;;
stop)
    echo -n "Stopping $DESC: $NAME"
    d_stop
    echo "."
    ;;
reload)
    echo -n "Reloading $DESC configuration..."
    d_reload
    echo "reloaded."
    ;;
restart)
    echo -n "Restarting $DESC: $NAME"
    d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
    sleep 2
    d_start
    echo "."
    ;;
*)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
    exit 3
    ;;
esac
exit 0

配置

cp /usr/local/nginx/conf/nginx.conf /opt/app/config/

sudo vim /etc/init.d/nginx
#超考上边脚本nginx
chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

启动

sudo service nginx start
sudo service nginx restart
sudo service nginx status
sudo service nginx stop

原始命令操作

sudo /usr/local/nginx/sbin/nginx -c /opt/app/config/nginx.conf
#或者
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload

配置代理

location / {
    proxy_pass http://192.168.0.1/project/;
    proxy_cookie_path  /project/ /;
    proxy_http_version 1.1;
    proxy_set_header Host $proxy_host;
    proxy_set_header Real-Host $host; #保留真实域名
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#设置下面两行即可(websock参透)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

针对大文件配置

proxy_send_timeout   90;
proxy_read_timeout   1800;
client_max_body_size    2048m;
#proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响应超时时间
#proxy_read_timeout:连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
#proxy_send_timeout :后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

压缩

  # gzip config
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

config配置

server {
		listen       80;
		server_name  你的域名;

		#后台服务配置,配置了这个location便可以通过http://域名/project/xxxx 访问		
		location ^~ /project {
			proxy_pass              http://127.0.0.1:8080/project/;
			proxy_set_header        Host 127.0.0.1;
			proxy_set_header        X-Real-IP $remote_addr;
			proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
		}
		#解决Router(mode: 'history')模式下,刷新路由地址不能找到页面的问题
		location / {
			root   html;
			index  index.html index.htm;
			if (!-e $request_filename) {
				rewrite ^(.*)$ /index.html?s=$1 last;
				break;
			}
		}
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值